I have 3 variables for a day and time of the week stored in an array :
$shift['day'];
$shift['hour'];
$shift['meridian'];
All 3 together, respectively output something like :
Friday 10 PM
DATE is not being used at all, just DAY and TIME and it's obviously not stored as a timestamp.
How can I check if this DAY and TIME has already passed THIS week?
So for example :
$today = date("l"); // Get current day "Monday"
$hour = date("g"); // Get current hour "3"
$meridian = date("A"); // Get current meridian "PM"
Will get me the current values ready to compare to my variables but I'm lost at how the logic would work to determine if my time has already passed this week or not?
Any help greatly appreciated.
You can use the date
functions to get the values down to integers then just use a conditional.
$shift['day'] = 'Friday';
$shift['meridian'] = 'PM';
$hour = '10';
$shift['hour'] = ($shift['meridian'] == 'PM') ? $hour + 12 : $hour; //convert input to 24 hour
$today = date("N");
$current_hour = date("G");
if( date('N', strtotime($shift['day'])) <= $today && $current_hour <= $shift['hour']) {
echo 'It hasn\'t occurred yet this week';
} else {
echo 'It has occurreced this week';
}
... or as a one liner (assuming $shift['hour']
is 24 hours):
if( date('N', strtotime($shift['day'])) <= date("N") && date("G") <= $shift['hour']) {
Demo: https://eval.in/542298
Make sure you have the timezone set correctly otherwise where midnight falls could throw off the day.
Logic:
DateTime
object for nowDateTime
object for Friday 10 PM
.Code:
$tz = new \DateTimeZone("UTC");
$now = new \DateTime("now", $tz);
$then = \DateTime::createFromFormat('l g A', 'Friday 10 PM', $tz);
if($then->getTimestamp() < $now->getTimestamp())
{
echo 'Friday 10 PM has passed this week';
}
else
{
echo 'No, Friday 10 PM has not passed this week';
}
Echoing $then->format('d.m.Y, H:i:s')
yields 25.03.2016, 22:00:00
. Changing the day from "Friday" to "Saturday" correctly yields 26th of March, which is what I used to verify that the DateTime
object is created correctly for given string (Friday 10 PM
).
The DateTime class provides a lot of tools to help in doing this, and another answer already utilizes it, but it's biggest advantage by far is that it actually allows you to work with dates in a purely human way - you don't have to feed it made-up numbers that just happen to make mathematical sense!
So here's one way to do that ...
Obviously, we need to create the timestamps to compare first:
// With no arguments, this is just the current timestamp
$now = DateTime();
// Tricky part here is to know that any omitted *calendar-type* values
// default to the current day, while *time* values default to 0s
$shift = DateTime::createFromFormat(
'l g A',
"{$shift['day']} {$shift['hour']} {$shift['meridian']}"
);
But what is hard to spot (at least at the time I'm writing this) in the PHP manual for DateTime
class is the DateTime::diff() method which actually only appears on the TOC for DateTimeInterface
.
Once you have that, you only need to know that +
and -
signs mean "future" and "past":
if ($now->diff($shift)->format('%R') === '-')
{
// $shift is in the past; i.e. Friday 10 PM has passed
}
Of course you can replicate the same thing via date()
, strtotime()
and math, but this is way more expressive and easily understandable.