PHP supports date functions to handle date & time to implement and provide a method to convert human-readable dates to Unix Timestamp. One of those methods is strtotime()
.
You can also check other recommended Date PHP tutorials,
The strtotime() function in PHP translates an English textual date-time description to a UNIX timestamp. The strtotime() function takes an English string as an argument that represents the date-time information.
strtotime(time, now);
Whereas:
– time : It specifies a date/time string.
– now : It is an optional parameter.The now parameter specifies the timestamp used to calculate the returned value.
echo strtotime("now"), "\n"; echo strtotime("11 March 2022"), "\n"; echo strtotime("+1 day"), "\n"; echo strtotime("+2 week"), "\n"; echo strtotime("next Friday"), "\n"; echo strtotime("last Saturday"), "\n";
Output:
1646974188 1646956800 1647060588 1648183788 1647561600 1646438400
echo date("Y-m-d", strtotime("now"));
Output:
2022-03-11
echo date("Y-m-d", strtotime("2022-03-11 +2 month"));
Output:
2022-05-11
echo date("Y-m-d", strtotime("2022-03-11 -2 month"));
Output:
2022-01-11
$textDate = "11th March 2022"; echo strtotime($textDate), "\n"; echo date("Y-m-d", strtotime($textDate))."\n";
Output:
1646956800 2022-03-11
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Hi, thanks for the tip about strtorime. When adding times, what about working days instead of current days... Actually I'm using third part js add-ons (luxon). Thanks !
$myDate = "2022-03-16";
$nDays = 10;
$newDate = strtotime($myDate . '+ '.$nDays.'days');