Shree krishna lamichhane
Shree Krishna Lamichhane

Shree Krishna Lamichhane

Formatting the Current Date and Time in PHP

Shree krishna lamichhane
·May 21, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Getting the Date and Time in String Format
  • Get the Unix Timestamp

Original Blog Link : https://code.tutsplus.com/tutorials/working-with-date-and-time-in-php--cms-31768

By Monty Shokeen on Feb 26, 2021

You'll often want to work with dates and times when developing websites. For example, you might need to show the last modified date on a post or mention how long ago a reader wrote some comment. You might also have to show a countdown of the days until a special event.

Luckily, PHP comes with some built-in date and time functions which will help us do all that and much more quite easily.

This tutorial will teach you how to format the current date and time in PHP. You will also learn how to get the timestamp from a date string and how to add and subtract different dates.

Getting the Date and Time in String Format

date($format, $timestamp) is one of the most commonly used date and time functions available in PHP. It takes the desired output format for the date as the first parameter and an integer as a timestamp value which needs to be converted to the given date format. The second parameter is optional, and omitting it will output the current date and time in string format based on the value of $format.

The $format parameter accepts a series of characters as valid values. Some of these characters have straightforward meanings: Y gives you the full numeric representation of the year with 4 digits (2018), and y only gives you the last two digits of the current year (18). Similarly, H will give you the hour in 24-hour format with leading zeros, but h will give you the hour in 12-hour format with leading zeros.

Here are some of the most common date format characters and their values.

CharacterMeaningExample
dday of the month with leading zeros03 or 17
jday of the month without leading zeros3 or 17
Dday of the week as a three-letter abbreviationMon
lfull day of the weekMonday
mmonth as a number with leading zeros09 or 12
nmonth as a number without leading zeros9 or 12
Mmonth as a three-letter abbreviationSep
Ffull monthSeptember
ytwo-digit year18
Yfull year2018

There are many other special characters to specify the output for the date() function. It is best to consult the format characters table in the date() function documentation for more information about special cases.

Let's see some practical examples of the date() function now. We can use it to get the current year, current month, current hour, etc., or we can use it to get a complete date string.

<?php

// Output — 2018
echo date('Y');

// Output — September 2018
echo date('F Y');

// Output — 13 September, 2018
echo date('d F, Y');

// Output — 13 September, 2018 (Thursday)
echo date('d F, Y (l)');

?>

You can also use the date() function to output the time. Here are some of the most commonly used time format characters:

CharacterMeaningExample
ghours in 12-hour format without leading zeros1 or 12
hhours in 12-hour format with leading zeros01 or 12
Ghours in 24-hour format without leading zeros1 or 13
Hhours in 24-hour format with leading zeros01 or 13
aam/pm in lowercaseam
Aam/pm in uppercaseAM
iminutes with leading zeros09 or 15
sseconds with leading zeros05 or 30

And here are some examples of outputting formatted time strings.

<?php

// Output — 11:03:37 AM
echo date('h:i:s A');

// Output — Thursday, 11:04:09 AM
echo date('l, h:i:s A');

// Output — 13 September 2018, 11:05:00 AM
echo date('d F Y, h:i:s A');

?>

It is also important that you escape these special characters if you want to use them inside your date string.

<?php

// Output — CEST201813am18 1115 Thursday.
echo date('Today is l.');

// Output — Today is Thursday.
echo date('\T\o\d\a\y \i\s l.');

// Output — Today is Thursday.
echo 'Today is '.date('l.');

?>

Get the Unix Timestamp

Sometimes, you will need to get the value of the current Unix timestamp in PHP. This is very easy with the help of the time() function. It returns an integer value which describes the number of milliseconds that have passed since 1 January 1970 at midnight (00:00:00) GMT.

You can also use this function to go back and forth in time. To do so, all you have to do is subtract the right number of seconds from the current value of time() and then change the resulting value into the desired date string. Here are two examples:

<?php

$ten_days_later = time() + 10*60*60*24;
// Output — It will be Sunday 10 days later.
echo 'It will be '.date('l', $ten_days_later).' 10 days later.';

$ten_days_ago = time() - 10*60*60*24;
// Output — It was Monday 10 days ago.
echo 'It was '.date('l', $ten_days_ago).' 10 days ago.';

?>

One important thing you should remember is that the timestamp value returned by time() is time-zone agnostic and gets the number of seconds since 1 January 1970 at 00:00:00 UTC. This means that at a particular point in time, this function will return the same value in the US, Europe, India, or Japan.

Another way to get the timestamp for a particular date would be to use the mktime($hour, $minute, $second, $month, $day, $year) function. When all the parameters are omitted, this function just uses the current local date and time to calculate the timestamp value. This function can also be used with date() to generate useful date and time strings.

<?php

$some_time = mktime(1, 1, 1, 12, 3, 1994);
// Output — It was Saturday on 03 December, 1994.
echo 'It was '.date('l', $some_time).' on '.date('d F, Y', $some_time).'.';

?>

Basically, time() can be used to go back and forth to a period of time, while mktime() is useful when you want to go to a particular point in time.

 
Share this