Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Date and Time

The PHP date() function formats a timestamp into a more human-readable date and time.

Syntax

date(format,timestamp)

 

Parameter

Description

format

Required. Specifies the format in which the timestamp will be displayed.

timestamp

Optional. Specifies a timestamp to format. Defaults to the current date and time if not provided.

A timestamp is a sequence of characters that represents the date and/or time when a specific event took place.

Get a Date

The required format parameter of the date() function determines how to format the date (or time).

Here are some commonly used characters for dates:

  • d – Represents the day of the month (01 to 31)
  • m – Represents the month (01 to 12)
  • Y – Represents the year (in four digits)
  • l (lowercase ‘L’) – Represents the day of the week

Other characters, such as “/”, “.”, or “-“, can be added between these characters for additional formatting.

The example below shows today’s date formatted in three different ways:

Example

<?php
echo “Today is “ . date(“Y/m/d”) . “<br>”;
echo “Today is “ . date(“Y.m.d”) . “<br>”;
echo “Today is “ . date(“Y-m-d”) . “<br>”;
echo “Today is “ . date(“l”);
?>

PHP Tip – Automatic Copyright Year

Use the date() function to automatically update the copyright year on your website:

Example

&copy; 2010-<?php echo date(“Y”);?>

Create a Date With mktime()

The optional timestamp parameter in the date() function specifies a particular timestamp. If it is not provided, the current date and time will be used (as shown in the examples above).

The PHP mktime() function returns the Unix timestamp for a given date. This timestamp represents the number of seconds elapsed between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the specified time.

Syntax

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

The example below uses the mktime() function to generate a timestamp from several parameters and then creates a date and time with the date() function:

Syntax

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

The example below uses several parameters from the mktime() function to generate a timestamp, which is then used by the date() function to create a date and time.

Example

<?php
$d=mktime(1114548122014);
echo “Created date is “ . date(“Y-m-d h:i:sa”, $d);
?>

Create a Date From a String With strtotime()

The PHP strtotime() function converts a human-readable date string into a Unix timestamp, which represents the number of seconds since January 1, 1970, 00:00:00 GMT.

Syntax

strtotime(time, now)

The example below generates a date and time using the strtotime() function.

Example

<?php
$d=strtotime(“10:30pm April 15 2014”);
echo “Created date is “ . date(“Y-m-d h:i:sa”, $d);
?>

PHP is quite flexible when converting a string to a date, allowing you to use various date formats and values.

Example

<?php
$d=strtotime(“tomorrow”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;

$d=strtotime(“next Saturday”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;

$d=strtotime(“+3 Months”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;
?>

However, strtotime() is not infallible, so be sure to verify the strings you pass to it.

More Date Examples

The example below displays the dates for the next six Saturdays.

Example

<?php
$startdate = strtotime(“Saturday”);
$enddate = strtotime(“+6 weeks”, $startdate);

while ($startdate < $enddate) {
  echo date(“M d”, $startdate) . “<br>”;
  $startdate = strtotime(“+1 week”, $startdate);
}
?>

The example below calculates and displays the number of days remaining until the 4th of July.

Example

<?php
$d1=strtotime(“July 04”);
$d2=ceil(($d1-time())/60/60/24);
echo “There are “ . $d2 .” days until 4th of July.”;
?>