PHP Compare Time

PHP compare time

$ThatTime ="14:08:10";
if (time() >= strtotime($ThatTime)) {
echo "ok";
}

A solution using DateTime (that also regards the timezone).

$dateTime = new DateTime($ThatTime);
if ($dateTime->diff(new DateTime)->format('%R') == '+') {
echo "OK";
}

http://php.net/datetime.diff

PHP - Comparing time

If those times are in the database, comparison operator of the database would works. For example:

SELECT * FROM table WHERE time < NOW()

In PHP, the easiest way to compare times is to convert them to timestamps, and then to compare timestamps as integers. You can use strtotime to do that conversion.

For example:

$time1 = "08:00:00";
$time2 = "09:00:00";

if (strtotime($time1) > strtotime($time2) ||
strtotime($time1) < strtotime("08:00:00")) {
...
}

Comparing time in PHP

date_default_timezone_set('Asia/Manila');

$deadtime = strtotime("15:00:00");

if ($deadtime - time() > 0){
echo '<td><a href="stud_prep.php">upload</a></td>';
}else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}

This may solve your time comparison issue!!

strtotime("15:00:00") will convert time into unix timestamp of today's 15:00:00 and time() have current unix timestamp.

How do I compare current time to a preset time in PHP?

strtotime depends on the time zone.. so you should define timezone too.
You should set your default timezone before comparing.

http://php.net/strtotime

Example:

date_default_timezone_set('America/Los_Angeles');

$eightthirtyam = "08:30:00";
if(time() >= strtotime($eightthirtyam )){
$refresh = true;
}

http://codepad.org/GC0VA7nw

How do I compare two DateTime objects in PHP 5.2.8?

The following seems to confirm that there are comparison operators for the DateTime class:

dev:~# php
<?php
date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
?>
bool(false)
bool(true)
bool(false)
dev:~# php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
dev:~#

PHP: How to compare a time string with date('H:i')?

You can use this:

$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
// do something
}

PHP compare between two times 24 hour format after 00:00 and before

Since you are only comparing the current time (time right now), logically you don't have to worry about tomorrow's time. You only need to compare if you are inside/outside today's opening/closing times.

<?php
// SET RELEVANT TIMEZONE
date_default_timezone_set('Europe/Dublin');

// CURRENT UNIX TIMESTAMP
$time_now = time();

// TODAY AT 18:00:00 (24 HOUR) UNIX TIMESTAMP
$opening_time = DateTime::createFromFormat('H:i:s', '18:00:00')->format("d-M-Y H:i:s"); // 11-May-2018 18:00:00

// TODAY AT 01:30:00 (24 HOUR) UNIX TIMESTAMP
$closing_time = DateTime::createFromFormat('H:i:s', '01:30:00')->format("d-M-Y H:i:s"); // 11-May-2018 01:30:00

// WE ARE CLOSED IF:
// TIME NOW IS AFTER CLOSING TIME TODAY (01:30:00)
// AND TIME NOW IS BEFORE OPENING TIME TODAY (18:00:00)
if($time_now > strtotime($closing_time) && $time_now < strtotime($opening_time))
{
echo "Sorry, we are closed!";
}
else
{
echo "We are open, come on in!";
}


Related Topics



Leave a reply



Submit