I am a newbie programmer, I would like to know how I can subtract two dates
My sample is
$datetoday = date("Y/m/d");
$dateprev = "2015/11/03";
All I want is, if $datetoday
is greater than $dateprev
, that the output is a positive value.
If $dateprev
is greater than $datetoday
, the output should be a negative value.
You can use strtotime function on those dates to turn them into integers (unix/epoch timestamp) and compare them that way.
And the time() function will get the current date/time directly as the unix timestamp.
You can use these code for date difference :-
<?php
$val1 = '2014-03-18 10:34:09.939';
$val2 = '2014-03-18 10:34:09.940';
$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);
if($datetime1 > $datetime2)
echo "1 is bigger";
else
echo "2 is bigger";
?>
You could use diff from the DateTime Interface. Here is a code example:
$datetoday = new DateTime();
$dateprev = new DateTime('2015/11/03');
$interval = $dateprev->diff($datetoday);
echo $interval->format('%R%a');
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.