开发者

optimizing date() and strtotime() function calls in php

开发者 https://www.devze.com 2023-02-18 15:11 出处:网络
How can I optimize this: $enddatetime = date(\"YmdHis\", strtotime(d开发者_如何转开发ate(\"YmdHis\", strtotime($session_date.\' \'.$session_time)) . \" + \".$session_duration.\" minutes\"));

How can I optimize this:

$enddatetime = date("YmdHis", strtotime(d开发者_如何转开发ate("YmdHis", strtotime($session_date.' '.$session_time)) . " + ".$session_duration." minutes"));

eg values:

$session_date 2011-01-31
$session_time 19:30:00
$session_duration 100


Ok, so basically you're taking a cow, turning it into hamburger, sprinkling on a little bit of salt, then gluing the salted hamburger back into a cow yet again.

Why the repeated roundtrips through date->strtotime->date, etc...? That's highly inefficient. PHP's native date/time format is a Unix-style timestamp - seconds since Jan 1/1970.

$timeval = strtotime($session_date . ' ' . $session_time);
$adjusted_timeval = $timeval + ($session_duration * 60);
$fancy_datetime = date("YmdHis", $adjusted_timeval);


Best i can think of is:

$enddatetime = date("YmdHis", strtotime($session_date.' '.$session_time) + (60*$session_duration));
0

精彩评论

暂无评论...
验证码 换一张
取 消