Page 1 of 1
PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:28 pm
by JAB Creations
As I have been working on creating my own blog software I needed to determine an way to convert MySQL's NOW() date/time format.
Functions are great because they are reusable so I decided since I use different date/time formats why not pass the date and format as parameters?
The function simply returns the value (you'll have to echo the function where you need it). You call the function with two variables: the first being the MySQL NOW() formatted date/time and the second the format for the time stamp.
To construct a time stamp to change date and time formatting (in example between 12, Dec, and December) simply use
http://php.net/date as a reference guide.
Suggestions for improvement are welcome. I adapted this from
rana_0036's php.net comment at
http://php.net/time.
Code: Select all
function date_mysql($date, $format)
{
$d = explode("-", $date);
$time = explode(" ", $d[2]);
$t = explode(":", $time[1]);
$datetime_converted = date($format, mktime ($t[0],$t[1],$t[2],$d[1],$d[2],$d[0]));
return $datetime_converted;
}
echo date_mysql("2008-11-03 14:53:12", "l F jS, Y, h:i A");
//Output: Monday November 3rd, 2008, 02:53 PM
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:32 pm
by andyhoneycutt
Have you tried strtotime()? It's pretty solid in my experience. I'm not sure how it would compare to your function in terms of processing time, but I'll run some tests and post the difference here.
-Andy
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:33 pm
by VladSun
Code: Select all
function date_reformat($date, $format)
{
return date ($format, strtotime($date));
}
EDIT: ++ andyhoneycutt

Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:42 pm
by andyhoneycutt
Results for each running 30,000 times:
date_mysql: 15.1370699406 seconds
strtotime: 13.8935859203 seconds
A nominal difference if you ask me, but it would depend on your application of the function(s). I'd like to see other people's approaches to your problem.
-Andy
The code:
Code: Select all
<?php
function date_mysql($date, $format)
{
$d = explode("-", $date);
$time = explode(" ", $d[2]);
$t = explode(":", $time[1]);
$datetime_converted = date($format, mktime ($t[0],$t[1],$t[2],$d[1],$d[2],$d[0]));
return $datetime_converted;
}
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
for( $i=0; $i<30000; $i++ )
{
$d = date_mysql("2008-11-03 14:53:12", "l F jS, Y, h:i A");
}
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "date_mysql: $totaltime<br />";
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
for( $i=0; $i<30000; $i++ )
{
$d = date("l F jS, Y, h:i A",strtotime("2008-11-03 14:53:12"));
}
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strtotime: $totaltime<br />";
?>
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:45 pm
by JAB Creations
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Thu Nov 06, 2008 1:48 pm
by JAB Creations
...and I love PHP.

Thanks.

Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Tue Nov 11, 2008 5:36 am
by JayBird
Why would you do this in PHP and not just grab the date as you needed it directly from MySQL?
Code: Select all
SELECT DATE_FORMAT(NOW(), '%W %M %Y')
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Sat Jan 10, 2009 2:27 pm
by jason.carter
Why dont you try this simply as this
EG: for date tomorrow.
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
You can format the date to whatever you want. Combine this with mktime you can select any date or even time.
Re: PHP function to convert MySQL NOW() format to ANY format
Posted: Mon Jan 12, 2009 10:49 am
by andyhoneycutt
jason.carter wrote:Why dont you try this simply as this
EG: for date tomorrow.
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
You can format the date to whatever you want. Combine this with mktime you can select any date or even time.
what happens when date("d")+1 comes out to something that would be in the next month? for instance, if it's 32, $tomorrow would be an invalid date.