date - PHP check if two Datetimes are not on the same calendar day -


i have 2 datetimes (the dates being $vars)

$starttime = \datetime::createfromformat('y/m/d h:i', '2015/01/01 23:00'); $endtime = \datetime::createfromformat('y/m/d h:i', '2015/01/02 01:00'); 

i struggle (possibly pretty) simple problem: how determine if 2 dates on different calendar days?

i cannot < 2015/01/01 22:00 < 2015/01/01 23:00 true. can not this:

$diff = $starttime->diff($endtime); $days = $diff->format('%d');  echo $days; 

as gives me 0.

this gives me idea how it, javascript, equivalent php?

//update

    $startdate = $starttime->format('y/m/d');     $enddate = $endtime->format('y/m/d');     $diffdates = $startdate->diff($enddate);     $daysdiff = $diffdates->format('%d');      echo $daysdiff; 

i think might right approach now, comments, error: call member function diff() on string

//update clarification i'm trying do

i want have difference in days, above '1' (although 2 hours difference actually) , example '2015/01/01 23:00' , '2015/01/03 17:00' '2'.

just create dates time set 00:00:00:

$starttime = \datetime::createfromformat('y/m/d h:i:s', '2015/01/01 00:00:00'); $endtime = \datetime::createfromformat('y/m/d h:i:s', '2015/01/02 00:00:00'); 

or reset time 0 on existing dates:

$starttime->settime(0, 0, 0); $endtime->settime(0, 0, 0); 

then should work:

$diff = $starttime->diff($endtime); $days = $diff->format('%d');  echo $days; // 1 

bonus

if want work dates, remember set time 00:00:00 in createfromformat or reset settime. if won't provide time in createfromformat php set current time:

$date = datetime::createfromformat('y-m-d', '2016-01-21'); print $date->format('h:i:s'); //not 00:00:00 

to fix it, must either:

  • provide 00:00:00 time in format:

    $date = datetime::createfromformat('y-m-d h:i:s', '2016-01-21 00:00:00');

  • prefix date format exclamation mark , omit time, set time 00:00:00 automatically:

    $date = datetime::createfromformat('!y-m-d', '2016-01-21');

  • reset time after creation:

    $date = datetime::createfromformat('y-m-d', '2016-01-21'); $date->settime(0, 0);


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -