strtotime() function: Calculation to Current Date and Time & Custom Date and Time in PHP

Date Published: 08/04/2020 Published By: JaiSchool

The strtotime() function returns the current date and time. Besides, returning the current date it also can be used for creating custom date and time. By using this function, we can add or remove days, weeks, years & months or hours, minutes & seconds to custom date and time or current date and time

We use some of the string parameters for returning that. Which are the following:-

But this function returns the date & time in UNIX format. When you print it you will some numbers that are not understandable. Therefore, you need to take the help of date() function which would convert this UNIX format to an actual date & time.

The second parameter of the strtotime() function contains the custom date & time (only used when we create custom date and time). Click here to scroll to example.

Do you know that the date function contains two arguments to pass the value into them? The first defines the format of the date in which it will be displayed.

And, the second is for UNIX date format.

<?php
date_default_timezone_set("Asia/kolkata");
$unix_time = strtotime("now");
echo $unix_time;

?>

When you run it you see something like this - 1586268895 The time is returned in UNIX format.

So, to make it in h:i:s (hours : minute : seconds), use this strtotime() function in the second parameter of the date() function. See the examples below.

<?php
date_default_timezone_set("Asia/kolkata");
$unix_time = strtotime("now");
$final_time = date("h:i:s",$unix_time);
echo $final_time;

?>

Note:*** You can set timezone according to your country.


Add or substract days to current date

Add day

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+5 days");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

You will see 5 days after date.

Sub day

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-5 days");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

5 days before date from current date.


Add or Remove months from current date

add 2 months

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+2 months");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

2 months added.

remove 2 months

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-2 months");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

2 months removed.


Add or Remove years to current date

Add years

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+4 years");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

4 years after date from current date

Remove years

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-4 years");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

4 years before date from current date


Add or remove hours to time

add 9 hours in current time

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+9 hours");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

9 hours added in current time.

remove 7 hours in current time

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-7 hours");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

7 hours removed from current time.


Add or remove minutes to current time

Add 5 minute

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+5 minutes");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

remove 9 minute

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-9 minutes");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Add or remove seconds to current time

Add 35 seconds in current time

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+35 seconds");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Remove 40 seconds from current time

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-40 seconds");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Add weeks to current date

Add 2 weeks

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("+2 weeks");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

remove 3 weeks

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-3 weeks");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Get next or last day of week to current date

When is the Next Tuesday?

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("next tuesday");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

When was Previous Sunday.

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("last sunday");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Another example 1

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("1 week 2 years 4 months 9 days 4 hours 2 minutes 25 seconds");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Another example 2

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("-3 week -6 years -2 months -7 days -4 hours 2 minutes 25 seconds");
$default_format = date("d-m-Y h:i:s A",$unix_format);
echo $default_format;

?>

Create custom date using strtotime() function

This function also used to create custom date and time similarly like date_create() function but in UNIX format. So here also, we will use date() function to convert Unix format into.

Example:-

12 hours format

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("11:08:47");
$default_format = date("h:i:s A",$unix_format);
echo $default_format;

?>

---

24 hours format

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("19:08:47");
$default_format = date("h:i:s A",$unix_format);
echo $default_format;

?>

It also can change 24 hours date format to 12 hours date format.


Addition in custom date created with strtotime() function

<?php
date_default_timezone_set("Asia/kolkata");
$unix_format = strtotime("09:08:47");
$default_format = date("h:i:s A",strtotime("1 hours",$unix_format));
echo $default_format;

?>

Similarly, you can add or remove weeks, days, months, years from custom date or hours, minutes and seconds form custom time.

Related Posts:-

Publish A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *