i'm getting date/time formats compare:
08:18:00 07/11/2013 7/11/2013 9:11:00
how convert each 1 of them format can write php find out 1 later date/time?
// $time1 in hh:mm:ss mm/dd/yyyy format. $time1 = "08:18:00 07/11/2013"; // $time2 in mm/dd/yyyy hh:mm:ss format. $time2 = "7/11/2013 9:11:00 am"; $time_one = $mytimeconvertdate_time_first($time1); $time_two = $mytimeconvertdate_date_first($time2); if ($time_two > $time_one) { echo "time_two later" . "\n"; } else { echo "time_one later" . "\n"; }
i have tried this, second value doesn't contain date info:
<?php date_default_timezone_set('america/new_york'); // $time1 in hh:mm:ss mm/dd/yyyy format. $time1 = "08:18:00 07/11/2013"; // $time2 in mm/dd/yyyy hh:mm:ss format. $time2 = "7/11/2013 9:11:00 am"; $dt1 = datetime::createfromformat("h:i:s m/d/y", $time1); $dt2 = datetime::createfromformat("m/d/y h:i:s", $time2); var_dump($dt1); var_dump($dt2); if ($dt1 < $dt2) echo "the first earlier"; ?>
the output is:
object(datetime)#1 (3) { ["date"]=> string(19) "2013-07-11 08:18:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "america/new_york" } bool(false)
corrected to:
<?php date_default_timezone_set('america/new_york');
// note: "one 24-hour format while other 12-hour am/pm. "
// $time1 in hh:mm:ss mm/dd/yyyy format. $time1 = "08:18:00 07/11/2013"; // $time2 in mm/dd/yyyy hh:mm:ss format. $time2 = "7/11/2013 9:11:00 am"; $dt1 = datetime::createfromformat("h:i:s m/d/y", $time1); $dt2 = datetime::createfromformat("m/d/y g:i:s a", $time2); var_dump($dt1); var_dump($dt2); if ($dt1 < $dt2) echo "the first earlier" . "\n"; ?> object(datetime)#1 (3) { ["date"]=> string(19) "2013-07-11 08:18:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "america/new_york" } object(datetime)#2 (3) { ["date"]=> string(19) "2013-07-11 09:11:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "america/new_york" } first earlier
use datetime::createfromformat
create 2 datetime
instances formatted text. can compare instances directly find earlier in time.
$dt1 = datetime::createfromformat("h:i:s m/d/y", $time1); $dt2 = datetime::createfromformat("m/d/y g:i:s a", $time2); if ($dt1 < $dt2) echo "the first earlier"; // etc
Comments
Post a Comment