PHP Date from String ( strtotime()
function) is a powerful and versatile tool available in the PHP programming language. It allows developers to easily convert a string into a date object, and then manipulate it in various ways. This is an important feature for many web applications, as it allows users to easily enter dates in a variety of formats, and then have the application process the string into a valid date.
strtotime() function
The PHP Date from String function is relatively simple to use. It takes a string as an argument and attempts to convert it into a date object. If successful, it will return a DateTime object, which can then be used to manipulate the date in a variety of ways. If the string cannot be converted into a valid date, then the function will return false.
strtotime(string $datetime, ?int $baseTimestamp = null)
The Date from String function is very versatile, as it can accept a wide range of date formats. For example, it can accept strings in the format of “MM/DD/YYYY”, “YYYY-MM-DD”, or “DD-MM-YYYY”. It can also accept strings in the format of “January 1, 2020”, “1st January 2020”, or “1/1/2020” and changes them to timestamp.
echo strtotime('02/16/2023');
Output
1676505600
If you want to return the timestamp value to the desired date, you must use the date()
function.
$timestamp = strtotime('02/16/2023');
echo date('Y-m-d', $timestamp);
Output
2023-02-16
The Date from String function can also accept a variety of time formats. For example, it can accept strings in the format of “HH:MM:SS”, “HH:MM”, or “HH”. It can also accept strings in the format of “12:00 AM”, “12:00 PM”, or “12:00”.
$timestamp = strtotime('02/16/2023 12:24:32');
echo date('Y-m-d H:m:s', $timestamp);
Output
2023-02-16 12:02:32
The Date from String function can also accept a variety of time zones. For example, it can accept strings in the format of “GMT”, “UTC”, or “EST”. It can also accept strings in the format of “America/New_York”, “Asia/Kolkata”, or “Europe/London”.
$dateStr = '2022-09-11 00:00:00';
$timezone = 'America/New_York';
$dtUtcDate = strtotime($dateStr. ' '. $timezone);
echo date('Y-m-d H:m:s',$dtUtcDate);
Output
2022-09-11 04:09:00
PHP Date From String to Number Format
The PHP date()
function is often used to format a date and time into a string. However, sometimes it is necessary to convert a date string into a numerical format. This can be done using the strtotime()
function, which converts a string into a Unix timestamp.
The strtotime()
function takes a string as an argument and attempts to convert it into a Unix timestamp. If successful, the function returns a Unix timestamp, which is a number representing the number of seconds since January 1, 1970. If the conversion is unsuccessful, the function returns false.
For example, the following code converts a date string into a Unix timestamp:
$date = '2021-02-14';
$timestamp = strtotime($date);
echo $timestamp;
Output
1613231600
The returned timestamp can then be used in other date functions, such as date()
or mktime()
.
In addition to converting a date string into a Unix timestamp, the strtotime()
function can also be used to convert a relative date string (e.g. “next Monday”) into a Unix timestamp. This is useful for calculating dates in the future or past.
For example, the following code will calculate the timestamp for the next Monday:
$next_monday = strtotime(‘next Monday’);
echo $next_monday;
Output
1613321600
The strtotime()
function is a powerful and versatile tool for converting strings into numerical date and time formats. It is an essential part of any PHP date and time manipulation.
Conclusion
The Date from String function is a powerful and versatile tool available in the PHP programming language. It allows developers to easily convert a string into a date object, and then manipulate it in various ways. This is an important feature for many web applications, as it allows users to easily enter dates in a variety of formats, and then have the application process the string into a valid date.