Page 1 of 1

date coversion query

Posted: Wed Sep 15, 2010 7:41 am
by IGGt
Hi Guys,
how can I get from a MySQL date (e.g. 2010-09-08 04:02:20) and output the day of week (e.g. Monday)?

I had the following suggested:

Code: Select all

print "</br>";print "</br>";print "</br>";

$a = date("2010-09-08 04:20:02");

$b = date("H,i,s,d,m,Y",$a);
print $b;

$c = mktime($b);
$d = date("F dS, Y", $c) ;
$e= date("l", $c) ;
Echo "$d is on a $e";
But this just gives me
Notice: A non well formed numeric value encountered in C:\wamp\www\test1.php on line 6
00,33,30,01,01,1970
Notice: A non well formed numeric value encountered in C:\wamp\www\test1.php on line 9
September 15th, 2010 is on a Wednesday

but that's todays date, not the date I input.

Re: date coversion query

Posted: Wed Sep 15, 2010 8:09 am
by buckit
This does what you want. I cleaned up some of your code, no reason to call print for every line break... you can just echo out all 3 in a row.

basically $sqlDate will have the value of the field in the database. then you want to convert it to something more usable using date. in this case I just changed the format to MM-DD-YYYY. now, one key thing you had missing was wrapping the value from the database in strtotime(). you need that to convert it to a valid date/time format otherwise date() wont understand it.

$theDate now has the converted value to MM-DD-YYYY, pretty much just ignoring the time because it doesnt seem you need it, but you can add that with no issue. next we call date() again to display the long name of the day based on the date given by $theDate.

Code: Select all


echo "<br /><br /><br />";

$sqlDate = "2010-09-08 04:20:02";
$theDate = date("m-d-Y", strtotime($sqlDate));

echo $theDate." is a ".date("l", $theDate);
	

 

Re: date coversion query

Posted: Wed Sep 15, 2010 8:37 am
by social_experiment

Code: Select all

<?php
// your mySQL date, i used today's date to check if it worked
$date = '2010-09-15 04:02:20';
	
// first explode the value into a two part array with the space as
// a delimiter
$array = explode(' ', $date);
	
// assign the first value in the array to variable
$date_ = $array[0];
	
// explode this value using the hypen as a delimiter
$array_ = explode('-', $date_);
	
// assign each of the values to a variable
$year = $array_[0];
$month = $array_[1];
$day = $array_[2];	
	
//echo gettype($year);
$new_date = mktime(0, 0, 0, $month, $day, $year);
	
echo date('l jS F Y', $new_date);
?>
Hth.

Re: date coversion query

Posted: Wed Sep 15, 2010 9:03 am
by IGGt
Cheers guys,

that all makes sense, I especially like the "$array = explode(' ', $date);" section as that has answered another question that I was pondering.


cheers,

Re: date coversion query

Posted: Wed Sep 15, 2010 9:21 am
by mikosiko
or .... to answer your original question
IGGt wrote:how can I get from a MySQL date (e.g. 2010-09-08 04:02:20) and output the day of week (e.g. Monday)?
you can just use in your MYSQL query any of the Date & Datetime functions.. like DATE_FORMAT($yourdate, format)

more to read here :
http://dev.mysql.com/doc/refman/5.1/en/ ... tions.html

and date_format() specifics here:
http://dev.mysql.com/doc/refman/5.1/en/ ... ate-format

Re: date coversion query

Posted: Wed Sep 15, 2010 9:32 am
by IGGt
You know, the most obvious answers are often the easiest to overlook. That has simplified things immensely.


cheers