Get current time and check if time has passed a certain period
Use the Java 8+ Time API class LocalTime
:
LocalTime refTime = LocalTime.of(13, 0); // 1:00 PM
// Check if now > refTime, in default time zone
LocalTime now = LocalTime.now();
if (now.isAfter(refTime)) {
// passed
}
// Check if now >= refTime, in pacific time zone
LocalTime now = LocalTime.now(ZoneId.of("America/Los_Angeles"))
if (now.compareTo(refTime) >= 0) {
// passed
}
How to check if the date is in the past in PHP?
Using strtotime()
:
if(strtotime($date)<strtotime("today")){
echo "past";
} elseif(strtotime($date)==strtotime("today")){
echo "today";
} else {
echo "future";
}
How can I check if the current date/time is past a set date/time in variable time zones?
Using DateTime, you can specify a time and time zone then compare to the current time:
$date = new DateTime('2017-05-05 07:00:00', new DateTimeZone('TimezoneID'));
return ($date->getTimestamp() < time());
PHP Check if date is past to a certain date given in a certain format
You can compare the dates with PHP's DateTime
class:
$date = new DateTime($event['date']);
$now = new DateTime();
if($date < $now) {
echo 'date is in the past';
}
Note: Using DateTime
class is preferred over strtotime()
since the latter will only work for dates before 2038. Read more about the Year_2038_problem.
PHP Checking if the current date is before or after a set date
if( strtotime($database_date) > strtotime('now') ) {
...
PHP check if date and time has passed 15 minutes
You can convert the date to a timestamp with strtotime
(which supports the MySQL date format) and then compare it to the current timestamp from time
.
$dbtimestamp = strtotime($datefromdb);
if (time() - $dbtimestamp > 15 * 60) {
// 15 mins has passed
}
To compare the dates, you can use date
to get the year/month/day from the timestamp and then compare them against the current date.
if (date("Y-m-d", $dbtimestamp) != date("Y-m-d")) {
// different date
}
PHP date time greater than today
You are not comparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015
> 01/02/2016
because 09
> 01
. You need to either put your date in a comparable string format or compare DateTime
objects which are comparable.
<?php
$date_now = date("Y-m-d"); // this format is string comparable
if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}
Demo
Or
<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");
if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}
Demo
Related Topics
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data
Decode Gzipped Web Page Retrieved Via Curl in PHP
Session Data Lost in Chrome Only
Why Doesn't MySQL Support Millisecond/Microsecond Precision
Why Is This PHP Call to Json_Encode Silently Failing - Inability to Handle Single Quotes
Prevent Sent Emails Treated as Junk Mails Using PHP Mail Function
Adding Months to a Date in JavaScript
How to Strip a Tag and All of Its Inner HTML Using the Tag'S Id
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
How to Use a String as an Array Index Path to Retrieve a Value
How to Split a String by Multiple Delimiters in PHP
If Statement in the Middle of Concatenation
How to Override the Path of PHP to Use the Mamp Path
Laravel Migration: Unique Key Is Too Long, Even If Specified