Geekpedia Programming Tutorials






How to convert date to time and back

This tutorial show how to convert DATE to TIME and customize output

On Wednesday, May 12th 2004 at 05:41 AM
By Nabeel Akhtar (View Profile)
*****   (Rated 4.1 with 28 votes)
Contextual Ads
More PHP Resources
Advertisement

MySQL spits out date in YYYY-MM-DD format. The following code will show how we can customize the output.

First thing we need to do is to convert the DATE to TIME:

<?
//Im just starting with  a date
$my_date '2004-01-12';

//First : Convert the date to time
$my_time strtotime($my_date);
?>



Yes, I have used the strtotime() function to convert the string $my_date to TIME. The strtotime() function will take an English date format and convert it to a UNIX timestamp.

Now that we have the TIME in $my_time, we can change the date output format to almost anything by using the DATE () function.

The following code will take the TIME in $my_time and display the date in YYYY-MM-DD format. [ This was the original format, but we are going to check if it really works ]

<?
//To display it as Y-m-d
echo date('Y-m-d',$my_time);
//This will give you 2004-01-12  (same as original)
?>



Note that we have used the DATE() function to convert the time to date.

Now if you would like to view the output as DD-MM-YYYY, use the following code

<?
//To display day-month-year
echo date('d-m-Y',$my_time);
//This will give you  12-01-2004

//To display the day, month spelled-out and Year
echo date("d , F Y"$my_time);
// This will give you  12 , January 2004
?>


This short tutorial was aimed to convert date to time and back to date in different formats. More information on PHP functions can be found at http://www.php.net
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by kris on Wednesday, March 21st 2007 at 08:39 AM

nice job... searched for an hour before i found this.

by b.b. goyal, Barnala on Sunday, April 1st 2007 at 03:55 AM

i was searching for date conversion in mysql for the last many days. your code is so simple, small and beautiful that i cant resist my urge to admire it. i am using it on my site http://www.punjabcolleges.com/. regards.

by pandi on Sunday, April 1st 2007 at 09:10 PM

nice

by x on Wednesday, April 18th 2007 at 01:33 PM

<marquee>good</marquee>

by Darshan Singla on Tuesday, June 19th 2007 at 04:33 AM

good

http://www.thenoida.com

by Rakesh Singhal on Tuesday, June 19th 2007 at 04:35 AM

very nice, i am looking for that.....

http;//www.haryanatravels.com

by WebMakerIndia on Tuesday, June 19th 2007 at 04:37 AM

Great!!!!!!!!!!!

http://www.webmakerindia.com

by Russ on Monday, August 6th 2007 at 07:13 PM

How do you convert an English dd/mm/yyyy to the American format?

by Rene on Sunday, August 19th 2007 at 05:02 AM

Thanks very much very clear tutorial.

by b.b.goyal on Tuesday, June 10th 2008 at 01:05 AM

kudos! keep it up. <a href="http://www.punjabcolleges.com">Nursing Colleges in Punjab</a>

by fahad on Monday, July 14th 2008 at 09:09 AM

its wow tutorial
i am really happy to find this
sometimes small things becomes more bigger and when we helped out so we feel glad
thanx

by fahad on Monday, July 14th 2008 at 09:09 AM

its wow tutorial
i am really happy to find this
sometimes small things becomes more bigger and when we helped out so we feel glad
thanx

by aqeel on Tuesday, July 22nd 2008 at 03:01 AM

good work.
really impressive.

by Daniel on Monday, September 29th 2008 at 07:07 AM

Thank's simple but effective

<a href="http://www.elancer.es">Trabajo Freelance</a>

by dfas on Wednesday, October 8th 2008 at 04:48 AM

.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs PHP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons