With its powerful classes and functions, PHP makes it easy to work with dates and time. In this lesson, we'll learn how to extract and format dates and time, and to convert between timestamps.

Creating a Date

Creating a date in PHP is super straightforward. Just use the built-in date() function.

PHP
<?php echo(date('d/m/Y')); ?>
HTML
09/01/2018

This returns the current date. However, you can also create a new date using your own values using the DateTime class. Afterwards, you can then format the object using the date_format() function by passing in the object, plus your desired format.

PHP
<?php $date = new DateTime('09/01/2018'); echo(date_format($date, 'd/m/Y')); ?>
HTML
09/01/2018

Formatting a Date

As for those letters used in the parameter, the date() function requires a format to return back the date. Here's a list of some of the characters you can use to get back a formatted string from a date:

  • d: Day of the month with leading zeros (01, 31)
  • j: Day of the month without leading zeros
  • D: Short text of day (Mon, Sun)
  • l: Full text of day (Monday, Sunday)
  • N: Number of day of the week (1, 7)
  • w: Number of day of the week (0, 6)
  • z: Day of the year (0, 365)
  • W: The week number (0, 52)
  • F: Full month name (January, December)
  • m: Month number with leading zeros (01, 12)
  • M: Short text of month (Jan, Dec)
  • n: Number of month without leading zeros (1, 12)
  • Y: Full year (1994, 2018)
  • y: Two digit year (94, 18)

Here's another example:

PHP
<?php echo('Today is ' . date('F j, Y') . '.'); ?>
HTML
Today is September 1, 2018.

Unix Time

In the field of computer science, there is this concept called Unix time. Unix time is the number of seconds that have elapsed since midnight on January 1st, 1970. With this frame of reference in mind, dates and times can be expressed in this new format.

For example, here is one billion seconds in Unix time:

PHP
<?php $timestamp = 1000000000; $date = new DateTime('@' . $timestamp); echo(date_format($date, 'd/m/Y')); echo(date('F d, Y h:i:s', $timestamp)); ?>
HTML
09/09/2001 September 09, 2001 01:46:40

If you noticed, you can also format the time using special characters. Here are all the characters you can use for time:

  • g: 12-hour number without leading zeros
  • G: 24-hour number without leading zeros
  • h: 12-hour number with leading zeros
  • H: 24-hour number with leading zeros
  • i: Minutes with leading zeros
  • s: Seconds with leading zeros
  • v: Milliseconds
  • u: Microseconds
  • a: Lowercase am or pm
  • A: Uppercase AM or PM

You can also get the current unix time by doing this:

PHP
<?php $timestamp = time(); ?>

mktime

The mktime(), standing for make time, lets you get a custom unix time by manually passing in values in the following syntax:

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

Here's a concrete example:

PHP
<?php echo(mktime(0, 0, 0, 9, 1, 2017)); ?>
HTML
1504224000

The cool thing about this format is that you can represent exact moments in time using a single number. Then you can use this number to convert to a readable string later down the road.

Resources

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.