Convert a Date to Timestamp in PHP

In this tutorial we will see How To Convert a Date to Timestamp in PHP. PHP strtotime() function can be used to convert date to timestamp, it parses an English textual datetime into a Unix timestamp.

PHP strtotime() function

PHP strtotime() function is used to parse English textual datetime into a Unix timestamp.

Unix timestamp is numbers of seconds from January 1 1970 00:00:00 GMT to the defined date. The Unix timestamp returned by the strtotime() doesn't provide information about the time zone.

See the examples given below which show how to use strtotime() function to convert date to timestamp.

In first example date given in Y-M-D format is converted into a timestamp.

<?php
$date = "2021-11-10";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1636502400

These are number of seconds from January 1 1970 to November 10 2021.

In second example a date in different format is converted to timestamp.

<?php
$date = "November 30 2021";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1638230400

In third example a date in American month, day and year format (12/22/78) is converted to timestamp.

<?php
$date = "11/25/21";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1637798400

In fourth example date in (last day) format is converted to timestamp.

<?php
$date = "last Sunday";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1636243200

This is how you can convert any date into timestamp, Only use allowed date formats with strtotime() function.

How To Remove Empty Array Elements with PHP How To Remove Common Values from two Arrays with PHP How To Find sum of array values with PHP How To Shuffle array Values in PHP How to Remove Duplicate Values from an array with PHP PHP Sort Associative array by Key PHP Sort Associative array by Value Sort array in numerical order with PHP Sort array in alphabetical order with PHP Sort array in descending order PHP Sort array in ascending order in PHP How to merge two or more arrays in PHP Add elements to the end of an array in PHP Add elements to the start of an array in PHP PHP Remove First Element from an Array PHP Remove Last Element from an Array Check if Key Exists in array PHP Check if Value exists in array PHP How to reverse an array in PHP PHP Print array line by line PHP Echo array values using foreach loop Count array values in PHP PHP Count How Many Times Word appears in String How to Reverse a String with PHP Check if Variable is Null in PHP