PHP has multiple functions to get and format date and time. Some of the most used are:

- These functions allow you to get the date and time from the server where your PHP scripts are running, they dependent on the locale settings of your server.

PHP date()

The PHP date() function is used to format a timestamp date/time.
  - Syntax:
date("format", timestamp)
- format   - A string format of the outputted date. Ex:   "D, d M Y H:i:s" => "Mon, 15 Aug 2012 15:42:01 +0000"
- timestamp   - An integer Unix timestamp. It's optional. If not added, defaults to the current local time.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
This function returns a formatted date string, or False on error.
• The complete list of format characters for date() function: Date Function Formatting.

- Example, get today date, month/day/year:
$today = date('m/d/Y');
echo $today;
By default, the date() function use the local server timezone, to set another default timezone, use the date_default_timezone_set('timezone_identifier'), prior to calling any of the date and time functions.
The "timezone_identifier" is a string with the timezone identifier (i.g 'America/New_York' or 'Europe/Lisbon'). The complete list of timezones supported by PHP is available in the List of Supported Timezones.
  - Example:
date_default_timezone_set('Pacific/Auckland');         // set the default timezone: Pacific/Auckland

// output the day of the week, of current time
echo date('l');

// output the month, day and the year, of current time
echo '<br />'. date('F j, Y');

// output the hour, minutes and seconds, of current time
echo '<br />'. date('H:i:s');
Outputs something like:
March 21, 2011

In the example above isn't specified a timestamp, so, the date() function uses the current date and time.

To get the current timestamp, use the time() function (i.g   $ts = time(); ).

Other examples with functions for Date and Time

If you want to get and use the Unix timestamp for a date, you can use the mktime() function.
  - Syntax:
mktime(hour, minute, second, month, day, year)
Returns the Unix timestamp corresponding the arguments given.

Also, you can use the strtotime() function. Parse an English textual datetime description into a Unix timestamp.
  - Syntax:
- The "time" parameter is a date/time string, in english literary form (i.g   "now", "+1 Day", "15 October 2012", "next Sunday", etc.). A list of valid formats is available in the Date and Time Formats.

  Example with mktime() and strtotime():
// get timestamp of October 15, 1976, 20:38:00 (with mktime)
// output the timestamp and the day of the week of this date
$stamp = mktime(20, 38, 0, 10, 15, 1976);
echo 'Timestamp: '. $stamp;
echo '<br /> October 15, 1976 is a '. date('l', $stamp);

// Example with strtotime()
echo '<br /><br />Example with strtotime():<br />';

echo '<br /> 15 October 1976, 20:38:00 - '. strtotime('15 October 1976, 20:38:00');
echo '<br /> 07 July 1996 - '. strtotime('07 July 1996');
echo '<br /> +3 days - '. strtotime('+3 days');
echo '<br /> +1 one week - '. strtotime('+1 week');
echo '<br /> +1 week 2 days 3 hours 30 seconds - '. strtotime('+1 week 2 days 3 hours 30 seconds');
echo '<br /> next Sunday - '. strtotime('next Sunday');
This code will output:
Timestamp: 214252680
October 15, 1976 is a Friday

Example with strtotime():

15 October 1976, 20:38:00 - 214252680
07 July 1996 - 836686800
+3 days - 1300950386
+1 one week - 1301292386
+1 week 2 days 3 hours 30 seconds - 1301476016
next Sunday - 1301176800

The 0 day of a month reprezent the last day of the previous month.
For example, 0 day of March 2012 is "29" the last day of February 2012:
                echo date('d', mktime(0, 0, 0, 03, 0, 2012));             // 29

• Another useful function for date and time is getdate()
  - Syntax:
- timestamp   - An integer Unix timestamp. It's optional. If not added, defaults to the current local time.
getdate() returns an associative array of information related to the timestamp, or the current local time if no parameter is given.
  - Example:
$now = getdate();
echo '<pre>';
echo '</pre>';
This code will output:
array (
  'seconds' => 48,
  'minutes' => 30,
  'hours' => 9,
  'mday' => 21,
  'wday' => 1,
  'mon' => 3,
  'year' => 2011,
  'yday' => 79,
  'weekday' => 'Monday',
  'month' => 'March',
  0 => 1300692648,

PHP has lots of functions for working with date and time. A complete list can be found to: Date/Time Functions
- For example microtime() returns the current Unix timestamp with microseconds. It can be used to find the execution time of a php script.
  - Code:
$load_time = -microtime(true);

// php code

$load_time += microtime(true);

echo $load_time. ' seconds';

Daily Test with Code Example

Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
 <li><a href="" title="CSS Course">CSS Course</a></li>
 <li><a href="" title="Flash Games">Flash Games</a></li>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
PHP Date and Time

Last accessed pages

  1. Ajax script to Save Canvas Image on Server (2753)
  2. Insert, Select and Update NULL value in MySQL (26448)
  3. SHA512 Encrypt hash in JavaScript (9801)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (71443)
  5. Register and show online users and visitors (22699)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (303)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (250)
  3. Register and show online users and visitors (241)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (238)
  5. Insert, Select and Update NULL value in MySQL (150)