PHP Date and Time

written by: Stratus Huo Quan; article published: year 2007, month 12;


In: Root » Computers and technology » PHP » PHP Date and Time

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

In this tutorial, we look at commonly asked questions regarding the date function in PHP.

How Do I Read the Date or Time from the Server?

To read the date or time from the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example:

<?php echo date("d/m/Y"); ?> outputs 11/02/2003  
<?php echo date("dS F, Y");?> outputs 11th February, 2003  
<?php echo date("g:i a"); ?> outputs 9:10 pm  
<?php echo date("H:i:s"); ?> outputs 21:10:48  
<?php echo date("l, dS F, Y"); ?> outputs Monday, 21st January, 2003  

Notice that you can put your own characters in the string that you pass to date, which can act as separators for the data, such as : or /. Using the tokens, you build up a date or time in any format you choose. It's worthwhile reading the manual page for the date function at the PHP online manual at http://www.php.net/manual/en/function.date.php.

How Do I Convert a Date to and from MySQL Format?

Another common question is how to work with dates and MySQL, as MySQL uses its own date format: yyyy-mm-dd. This means that if you want to display the date, you need to change the format to a more normal one, such as the U.S. date format: mm/dd/yyyy.

The easiest way to convert the date is to use the PHP explode function. The explode function takes a string and splits it by a common separator into an array. This is very useful for transforming dates, as dates have a common separator and so are easy to split into separate parts, which can then be rearranged into your desired format.

Note that you can also format the date in your SQL query using the MySQL DATE_FORMAT command, although this does make your SQL queries longer. Full details are available on the following page of the online MySQL manual: http://www.mysql.com/doc/en/Date_and_time_functions.html.

How Do I Convert a Date from U.S. Format to MySQL?

To convert a date from U.S. format to MySQL format, you can use the following function:

<?php  
function date2mysql($date){  
$splitArray = explode("/",$date);  
$newDate = $splitArray[2] . "-" . $splitArray[0] . "-" . $splitArray[1];  
return $newDate;  }  
?>  

This function takes a date in U.S. format and converts it to the MySQL format. To call this function, you just pass it the date in U.S. format, as follows:

<?php echo date2mysql("02/20/2003"); ?>  

This will output the following:

2003-02-20  

This can then be entered into a MySQL Date field. Note that to cater for other date formats, you can just rearrange the date parts into a different order.

How Do I Convert a Date from MySQL Format to U.S. Format?

To convert a date from MySQL format to U.S. date format, you can use the following function:

<?php  
function mysql2date($date){  
$splitArray = explode("-",$date);  
$newDate = $splitArray[1] . "/" . $splitArray[2] . "/" . $splitArray[0];  return $newDate;  
}  
?>  

To use this function, you call it by passing the date in MySQL format as follows:

<?php echo mysql2date("2003-02-20"); ?>  

This will output the following:

02/20/2003  

To use the function with a value from a recordset, simply use the field from the recordset as the parameter for the function, for example:

<?php echo mysql2date($row_Recordset1['date']); ?>  

This would convert a database field named date in the recordset named Recordset1 from MySQL format to the U.S. date format.

How Do I Find the Length of Time Between Two Dates?

In order to find the length of time between two dates, you need to convert both dates to a common format, and then you can take one date away from the other. The common format you use is the number of seconds since the Linux epoch date, which is 1/1/1970.To convert a date to the common format, you use the PHP mktime function, which takes the following parameters and returns the number of seconds since 1/1/1970:

mktime(hour, minute, second, month, day, year);  

You use the mktime function to convert the start date and end date into seconds, take the start date away from the end date, and then divide by the number of seconds in a day. You can do this with the following code:

<?php  
$startDate = "02/20/2003"; 
$endDate = "02/28/2003";  
$splitStart = explode("/",$startDate);  
$splitEnd = explode("/", $endDate);  
$start = mktime(0,0,0,$splitStart[0],$splitStart[1],$splitStart[2]);  
$end = mktime(0,0,0,$splitEnd[0],$splitEnd[1], $splitEnd[2]);  
$secondsInDay = 60 * 60 * 24;  
$days = abs($end-$start)/$secondsInDay;  
echo $days;  
?>  

When the preceding code is run, it will display 8, which is the number of days between $startDate and $endDate.

How Do I Find a Date, x Days/Months/Years in the Past or Future?

To find dates in the past or future, you can use a combination of the mktime and date functions, which allows you to easily add or subtract days, months, or years to/from the current date, as follows:

<?php  
$tomorrow  = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));  
$lastmonth = mktime (0,0,0,date("m")-1,date("d"), date("Y"));  
$nextyear  = mktime (0,0,0,date("m"), date("d"), date("Y")+1);  
echo date("m/d/Y", $tomorrow) . "<br>";  
echo date("m/d/Y", $lastmonth) . "<br>";  
echo date("m/d/Y", $nextyear) . "<br>";  ?>  

Any date can be found by simply adding or subtracting the desired period to/from one of the date parts, as shown in the preceding example code.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article