PHP function to convert MySQL NOW() format to ANY format

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP function to convert MySQL NOW() format to ANY format

Post 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
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PHP function to convert MySQL NOW() format to ANY format

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP function to convert MySQL NOW() format to ANY format

Post by VladSun »

Code: Select all

function date_reformat($date, $format)
{
   return date ($format, strtotime($date));
}
EDIT: ++ andyhoneycutt ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PHP function to convert MySQL NOW() format to ANY format

Post 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 />";
 
?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP function to convert MySQL NOW() format to ANY format

Post by JAB Creations »

Image
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP function to convert MySQL NOW() format to ANY format

Post by JAB Creations »

...and I love PHP. :roll: Thanks. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: PHP function to convert MySQL NOW() format to ANY format

Post 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')
 
jason.carter
Forum Commoner
Posts: 35
Joined: Sat Jan 10, 2009 10:05 am

Re: PHP function to convert MySQL NOW() format to ANY format

Post 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.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PHP function to convert MySQL NOW() format to ANY format

Post 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.
Post Reply