Php

Convert Datetime To TimeStamp Using PHP 7

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,

PHP strtotime

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.

Syntax

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.

Examples:

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

Get Current Date in PHP

echo date("Y-m-d", strtotime("now"));

Output:

Related Post

2022-03-11

Future Date in PHP

echo date("Y-m-d", strtotime("2022-03-11 +2 month"));

Output:

2022-05-11

Past Date in PHP

echo date("Y-m-d", strtotime("2022-03-11 -2 month"));

Output:

2022-01-11

strtotime to convert English text to a date

$textDate = "11th March 2022";

echo strtotime($textDate), "\n";

echo date("Y-m-d", strtotime($textDate))."\n";

Output:

1646956800
2022-03-11

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 !

Recent Posts

What is the Purpose of php_eol in PHP?

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

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories