php 5.2 - Calculating how many 'Midnights' is one date past another in PHP? -
i have start/end times calculation i'm trying , having problem seeing if end time before 12am day after start time. also, need calculate how many days past start time is.
what have: start date, end date
what need: - how many 'midnights' end date past start date?
has done this?
this uses php 5.3, if have earlier version may need use unix timestamps figure out difference. number of midnights should number of days difference assuming both start , end times have same time. setting both midnight of current day settime(0,0)
, should make calculation correct.
using datetime objects.
$start = new datetime('2011-03-07 12:23:45'); $end = new datetime('2011-03-08 1:23:45'); $start->settime(0,0); $end->settime(0,0); $midnights = $start->diff($end)->days;
without using settime()
calls, result in 0, because there less 24 hours between start , end. settime()
results in 1 because difference 24 hours.
the diff()
function introduced in 5.3 along dateinterval class. in 5.2 can still use datetime class have work out total days using unix timestamp.
$midnights = ($end->format('u') - $start->format('u')) / 86400
you can wrap in abs()
function order of start/end not matter.
note: these functions may need tested cases involve dst.
a comment in php date documentation uses round after dividing 86400 (number of seconds in day), counter issues involved dst.
an alternative approach datetimes create them in utc.
$utctimezone = new datetimezone('utc'); $start = new datetime('2011-03-07 12:23:45', $utctimezone); $end = new datetime('2011-03-08 1:23:45', $utctimezone);
Comments
Post a Comment