In: Categories » Computers and technology » PHP » PHP Date and Time
| 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.
|
legal disclaimer
1) Our website 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 infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial 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. Please read the Terms of service
Useful tools and features
related articles
PHP is finally getting the attention that i deserves, yes I have always believed that PHP is one of those neglected languages, neglected because they are used in abundance but there isn't enough programs or as we call them frameworks to work on PHP. But that was until the release of PHP 5. After the release of PHP, there is a range of Frameworks available. Today we review and understand closely the various frameworks available for PHP. Some of the most popular frameworks for PHP are: ...
2. Common PHP Errors
In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them. Parse Errors A parse error occurs when the format of your PHP code is incorrect. For example, the following code: <?php for($i=1;$i<10;$i++){ $output = "Current Iteration: " . $i . "<br>" echo $output; } ?> will return an error similar to the following: Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on lin...
3. PHP mail
In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as HTML. How Do I Send an E-mail As HTML? As the PHP mail function defaults to sending plain text e-mails unless otherwise specified, a frequent question is how to send HTML e-mails using the mail function. The format for the mail function is as follows: mail($to, $subject, $message, $headers); ...
4. PHP Random Password
In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function: <?php function randomPassword($length) { $possibleCharacters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $characterLength = strlen($possibleCharac...
5. How to Install PHP into Apache
In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get it from the source. Downloading PHP PHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3....
6. Installing PHP with Apache on Windows
We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache. Downloading PHP The first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php. Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and the...










