简体   繁体   中英

Calculate date based on given time

I went through a project which I need to set a recall time for an event. the recall time is set to 1 day and some hours before the event happen. I calculate event date and recall time like this:

$now = time();
$days_to_event = 24 ; //(I set an input for it in form)
$event_time = $now + $days_to_event * 3600;
$difference = 24*3600 + time('h')*3600 + time('i')*60;
$recall_time = $event_time - $difference;

I was wondering if I could create a function which takes $recall_time and calculates the date and outputs it.

Any useful idea for simplifying the project is a great help for me.

Thank you my friends.

我想你正在寻找的答案可能是

 echo date('Y-m-d H:i',$recall_time);

time() function return timestamp format, You need date() function to get hour and minute.

$now = time();
$days_to_event = 24; //(I set an input for it in form)
$event_time = $now + $days_to_event * 3600;
$difference = 24 * 3600 + date('h') * 3600 + date('i') * 60;
$recall_time = $event_time - $difference;

echo date('Y-m-d H:i', $now) . '<br>';
echo date('Y-m-d H:i', $event_time) . '<br>';
echo date('Y-m-d H:i', $recall_time) . '<br>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM