Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has THIS day and time passed THIS week or not - PHP

Tags:

php

time

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.

like image 935
Grant Avatar asked Dec 19 '25 19:12

Grant


2 Answers

Logic:

  • Create DateTime object for now
  • Create DateTime object for Friday 10 PM.
  • Compare them by timestamps to see which one is larger

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).

like image 143
Mjh Avatar answered Dec 22 '25 10:12

Mjh


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.

like image 43
Narf Avatar answered Dec 22 '25 08:12

Narf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!