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
PHP: Date larger than current date
Try converting them both to timestamps first, and then compare two converted value:
$curdate=strtotime('22-02-2011');
$mydate=strtotime('10-10-2011');
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This converts them to the number of seconds since January 1, 1970, so your comparison should work.
get the data is greater than is equal todays date
Your dates are not in a valid MySQL date format (the column is presumably a VARCHAR
or similar), and are being compared as strings. Since all your dates start with 1
, 2
, or 3
, they are all greater than (using a string comparison) a date string starting with 0
. You need to convert the formats to compare correctly:
SELECT *
FROM tbl_reservation_info
WHERE ri_id !=''
AND status = 'Success'
AND STR_TO_DATE(ri_arrival_date, '%d-%b-%Y') > STR_TO_DATE('08-Apr-2019', '%d-%b-%Y')
Comparing different date format than US to another date
Raül.
If you want to add days to a date, you should do it on the string and then convert it to DateTime:
If I got it right, you have 3 dates:
- The one that comes from
$item->getData('attribute_name');
as String - The user date from
$userDate = DateTime::createFromFormat('d/m/Y', $date);
as DateTime - The "+ 15 days" date, which you have to calculate.
Before converting your string to DateTime object (in $data = date_create(date('d/m/Y',strtotime($date)));
), you should calculate your "+ 15 days" date.
Here's how I would do it:
$datestring = '05/02/2019';
$date = date_create(date('d/m/Y',strtotime($datestring))); //first date
$userDate = DateTime::createFromFormat('d/m/Y', '22/02/2019'); //user date
if ($userDate === false) {
throw new InvalidArgumentException('Invalid date string');
}
$cmp = DateTime::createFromFormat('d/m/Y',date('d/m/Y', strtotime($date->format('Y/m/d').' + 15 days'))); //first date +15 days
if ($userDate <= $cmp && $userDate >= $date) {
$result = "The user date is in range";
}
else {
$result = "The user date is out of range";
}
echo('<b>First date: </b>'.$date->format('d/m/Y'));
echo('<br> <b>User date : </b>'.$userDate->format('d/m/Y'));
echo('<br> <b>Date + 15 : </b>'.$cmp->format('d/m/Y'));
echo('<br><br>'.$result);
Also, the ' + 15 days' concatenation only worked for me in Y/m/d format. That's why I formatted it to calculate.
In my example I compare the $userDate to check if it falls between the other two, but you can compare them as you wish.
Hope it helps
php greater than certain time
Should do it
if (((int) date('H', $currentTime)) >= 16) {
// .. do something
}
Because PHP is weak-typed you can omit the (int)
-casting.
As a sidenote: If you name a variable $currentTime
, you shouldn't add 1 hour to it, because then its not the current time anymore, but a time one hour in the future ;) At all
if (date('H') >= 16) { /* .. */ }
Related Topics
PHP Rename Array Keys in Multidimensional Array
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
Error_Reporting(E_All) Does Not Produce an Error
Connection Reset by Peer: Mod_Fcgid: Error Reading Data from Fastcgi Server
How to Bulk Update MySQL Data with One Query
Understanding PHP Curl_Multi_Exec
How to Remove Empty Paragraph Tags from String
In PHP How Does Usort() Function Works
Copy File from Remote Server or Url
PHP Session Ids -- How Are They Generated
Best Way to Internationalize Simple PHP Website
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
Add Class="Active" to Active Page Using PHP
Check for Duplicates Before Inserting
What Is the Postman-Token Header Attribute in Generated Code from Postman
How to Use Curl and PHP Simple HTML Dom Parser with Object
Apple Sign in "Invalid_Client", Signing Jwt for Authentication Using PHP and Openssl