I have logical problem with if...else
in PHP that using date in operation.
What I want is:
- if
$jd
less than$jam
printout 'red' - if between
$jam
and$jam
plus 2 hours printout 'green', - if
$jd
more than$h1
printout 'white'.
This is the source code:
date_default_timezone_set('Asia/Jakarta');
$jam = Date('H:i');
$jd = '12:00:00';
$h1 = $jd 2;
if ($jd > $h1){
echo 'white';
} elseif ($jd < $h1) {
if ($jd > $jam) {
echo 'green';
} else {
echo 'red';
}
}
The problem is the $jd
value more than $jam
plus 2 hours its printout 'green' instead 'white'.
@_@
It seems doesn't work with date operation, but with number its working.
Please kindly help?
CodePudding user response:
https://www.php.net/manual/en/datetime.add.php
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
You should add a given timespan using the date_add
function
CodePudding user response:
date_default_timezone_set('Asia/Jakarta');
$staticTime = '12:00:00';
$jam = strtotime(date('H:i'));
$jd = strtotime($staticTime);
$h1 = strtotime($jam . " 2hours");
if($jd < $jam) {
echo 'red';
} else if($jd > $jam && $jd < $h1 ) {
echo 'green';
} else if($jd > $h1) {
echo 'white';
}
convert the time to strtotime to compare