Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.3 with 38 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

.

by Martin on Tuesday, March 10th 2009 at 12:14 PM

Very good. Thank you!

by dharitri on Saturday, May 23rd 2009 at 02:09 AM

really good...n it works in my project.good

by phpmaster on Thursday, May 28th 2009 at 11:27 AM

i actually found a nicer solution here:

http://www.peeaitchpee.com/php/using-php-to-reformat-a-datetime-value-from-mysql/

the guy uses array functions to breakdown the datetime and it works perfectly

by Eds on Sunday, June 28th 2009 at 06:16 PM

Thanks for this #1 :)

by Ludwe on Sunday, July 26th 2009 at 07:05 AM

Thanx...! Nice and clean.

by hikey on Monday, October 12th 2009 at 05:20 AM

Thanks! Nice and clean and very helpful!

by nugroho on Monday, December 28th 2009 at 12:09 AM

thanx

by Abdul on Thursday, March 18th 2010 at 07:28 PM

Thanx a lot for this, it saved me bulk of time...

by ctr-ek on Tuesday, May 11th 2010 at 12:27 PM

Thanks, very hopeful!

by Karga on Tuesday, August 10th 2010 at 11:39 AM

<a href="http://burclar.in">Burçlar</a>


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 >>
Sponsors
Discover Geekpedia

Other Resources