How to get the next date with a specific time in PHP

I want to get a date with a specific day and time in PHP, for example, I want the date of the next day and time at 9.30 a.m. to be "2011-06-02 09:30:00". the code that I used to do this,

<?php
   $next_day_date = date("Y")."-".date("m")."-".(date("d")+1)."  09:30:00";
   $new_trig_time_stamp = strtotime($next_day_date);
   $trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
   echo $trigger_date_time;
?>

the above code works fine, but it doesn’t work for 31 days, on the 31st it returns "1970-01-01 05:30:00". Is there any other way to do this.

+3
source share
5 answers

When shifting dates by a fixed number, it is better to use mktime()it because it copes with incorrect dates well (for example, it knows that January 32 is actually February 1)

$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
+5
source

Calculate it through timestamp unix - much less annoyance

<?php
   $trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
   echo $trigger_date_time;
?>
+1

strtotime() .

$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
+1

date_add.

, - ...

$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));

$myDate->format(…) .

0
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
0

All Articles