-2

I want to do something like this:

If current time is > 19:30 and < 10:00 print yesterday's date [e.g. Thursday October 17th]

Else print today's date [e.g. Friday October 18th].

How can this be done with PHP?

4

1 回答 1

0

You can use createFromFormat() method to convert your time string into a DateTime object and do the comparison, like so:

$now = new DateTime('now');

$time1 = DateTime::createFromFormat('H:i', '19:30');
$time2 = DateTime::createFromFormat('H:i', '10:00');

if ($now > $time1 && $now < $time2) {
    echo $now->format('l F dS');
} else {
    $now->modify('-1 day');
    echo $now->format('l F dS');
}

Demo!

于 2013-10-18T22:10:52.397 回答