Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the unix timestamp for the start of the next day in php?

Tags:

I have a unix timestamp for the current time. I want to get the unix timestamp for the start of the next day.

$current_timestamp = time();
$allowable_start_date = strtotime('+1 day', $current_timestamp);

As I am doing it now, I am simply adding 1 whole entire day to the unix timestamp, when instead I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

What is the best way to go about this?

like image 244
zeckdude Avatar asked Mar 26 '10 00:03

zeckdude


People also ask

What is Unix timestamp in PHP?

What is the Unix timestamp? This is the number of seconds since the Unix epoch time (01-01-1970) . For more tips on the PHP date() function, we can see this post.

How can I timestamp in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

The most straightforward way to simply "make" that time:

$tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1);

Quote:

I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

Don't do it like that. Avoid relative calculations whenever possible, especially if it's so trivial to "absolutely" get the timestamp without seconds arithmetics.

like image 105
deceze Avatar answered Oct 21 '22 09:10

deceze


You can easily get tomorrow at midnight timestamp with:

$tomorrow_timestamp = strtotime('tomorrow');

If you want to be able to do a variable amount of days you could easily do it like so:

$days = 4;
$x_num_days_timestamp = strtotime(date('m/d/Y', strtotime("+$days days"))));
like image 35
tony4d Avatar answered Oct 21 '22 10:10

tony4d