Table of Contents
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'));
?>
HTML09/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'));
?>
HTML09/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 zerosD
: 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') . '.');
?>
HTMLToday 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));
?>
HTML09/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 zerosG
: 24-hour number without leading zerosh
: 12-hour number with leading zerosH
: 24-hour number with leading zerosi
: Minutes with leading zeross
: Seconds with leading zerosv
: Millisecondsu
: Microsecondsa
: Lowercase am or pmA
: 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:
PHPmktime(hour, minute, second, month, day, year);
Here's a concrete example:
PHP<?php
echo(mktime(0, 0, 0, 9, 1, 2017));
?>
HTML1504224000
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
- How to Install Node on Windows, macOS and Linux
- Create an RSS Reader in Node
- Getting Started with Electron
- How to deploy a .NET app using Docker
- Getting Started with Deno
- Getting Started with Sass
- Getting Started with Handlebars.js
- Learn how to build a Slack Bot using Node.js
- Creating a Twitter bot with Node.js
- Getting Started with React
- Getting Started with Vuex: Managing State in Vue
- How To Create a Modal Popup Box with CSS and JavaScript