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.
Table of Contents
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.