date coversion query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

date coversion query

Post 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.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: date coversion query

Post 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);
	

 
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: date coversion query

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: date coversion query

Post 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,
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: date coversion query

Post 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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: date coversion query

Post by IGGt »

You know, the most obvious answers are often the easiest to overlook. That has simplified things immensely.


cheers
Post Reply