Php dates is not compare properly - php

I am new in PHP
when I am trying to do this
if( date('m-Y',strtotime('2016-11-01 00:00:00')) < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
but it always do false [output 'no'].
I must need to compare months is less than current month , means compare date do not have same months
where I am wrong to compare that date ?

Use DateTime to compare dates:
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}

I copied your program so that it reads:
<?php
$x=date('m-Y',strtotime('2016-11-01 00:00:00'));
echo "$x\n";
$y=date("m-Y");
echo "$y\n";
if ($x < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
On running it the output is:
# php x.php
11-2016
01-2017
no
That is why it fails. If you are checking for just the month you need to check for equality. Otherwise you need to reorder the date formatting to be "Y-m" (not 'm-Y') for less/greater than comparisons. Comparing the strings is fine.

date function always return a string. In your if construct you compare two strings. For current time:
"11-2016" < "01-2017"
In this case "11-2016" greater than "01-2017".
It will be better to use DateTime class.
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
or in your example you need to change format to 'Y-m'.

You should use a decent format to compare the dates. Instead of m-Y, use Y-m-d.
Currently, you are converting the dates to strings, with their months first. So the first date becomes 11-2016, the second becomes 01-2017. PHP compares these as strings, and finds that 0 is less thans 1, so considers the second string to be less.

Related

how to understand if one date in php is less than another minus one day?

how to understand if one date in php is less than another minus one day? I mean if for example a date is set to "2018/07/03"; how can I understand if a given date is less than "2018/07/02"
date1 : year1/month1/day1
date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
the above code fails if forexample $year2 = 2017 and $month2 = 11.. can anybody help me? thanks a lot..
Here, this should work.
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
$date = strtotime("2018/07/01");
$date2 = strtotime("2018/07/02");
if($date > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger');
}
To convert string to date php has function named strtotime().
Compairing date objects is simple.
There is full information about strtotime()
http://php.net/manual/ru/function.strtotime.php
Another way:
$date = new DateTime("2018/07/01");
$date2 = new DateTime("2018/07/02");
if($date->modify("+1day") > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger or equal');
}
Notice modify modifies $date object itself.
Read more here http://php.net/manual/en/class.datetime.php

It's will be right ? for use < , > operator for compare date on php?

It's will be right ? for use < , > operator for compare date on php ?
I tested it. And it's work good. But i not founded documents for advice about use < , > operator for compare date on php.
Please tell me can we use < , > operator for compare date on php ?
<?PHP
$today = "2010-10-19";
$expired = "2012-12-10";
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
?>
As long as your dates are in a comparable format this will work. Since they are compared as strings you need to make sure that the larger dates components are first and the smaller ones last. For example, the year, then the month, then the date. Always include leading zeros. That's what you do in your example so it works.
If you do not have the dates in this format your comparison will fail some of the time. For example:
'2016-07-04' > '2016-01-07' // true
'04-07-2016' > '07-01-2016' // false
The best way to compare dates is with DateTime() as DateTime objects are comparable and also account for all things date related like leap year and DST.
<?PHP
$today = new DateTime("2010-10-19");
$expired = new DateTime("2012-12-10");
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
Use strtotime to convert datetime description into a Unix timestamp and you can easily compare timestamp with < or > operator
$today = strtotime('2010-10-19');// return 1287426600
$expired = strtotime('2012-12-10');// return 1355077800
if ($expired > $today) {// easily comparison of 1355077800 > 1287426600
echo "not expired";
} else {
echo "expired";
}

php find a stack of time

I need to find If $now(00:00:00) if included from $a and $b
echo $a= date('H:i:s', strtotime("23:00:00"));
echo $now = date('H:i:s', time("now");*(es.00:00:00)*
echo $b=date('H:i:s', strtotime("01:00:00"));
if ($now>$a && $now<$b) {echo "si";} else {echo "no";}
output say: no
__________________EDIT____________________________
Ok thanks to all, I think give you more information:
I read with php an file .xml, from this file, I get some information about program in tv, with :
-the name of program, -name of channel, -and time to program start in this format (20151210004500 +0100)
I think at this point is more easy to use this format, or not?
I would find in array from .xml the program on air, for make this I must compared the date and time in this format, It is possible?
Can you help me?
Sorry for my poor english, thanks.
Apply strtotime before comparation
if(strtotime($now) > strtotime($a) && strtotime($now) < strtotime($b)
{
// do your staff
}
$now = new DateTime;
$a = (new DateTime)->setTime(23, 0, 0);
if ($now > $a && $now < $a->modify('+2 hours')) {
echo 'si';
} else {
echo 'no';
}
You can easily do by using time() function to get the current time in numeric format (seconds from the 1970-1-1).
Than you can convert your dates into the same time format with strtotime() function.
$now = time();
$a = strtotime("10 september 2010");
$b = strtotime("10 september 2020");
if( $now > $a && $now < $b )
echo "Yes";
else
echo "No";

Less than operator not wokking in date

Less than operator not working while comparing date
$today="27-02-2015";
$end="24-06-2015";
if($end < $today){
echo "yes";
}else{
echo "no";
}
You are doing a string compare here, which does not tell you which date is later/earlier. You could change these dates into DateTime and compare them
$a = DateTime::createFromFormat('d-m-Y', '27-02-2015');
$b = DateTime::createFromFormat('d-m-Y', '24-06-2015');
if ($b < $a) {
echo " do something here";
}
change the string to format "2015-02-27" - then year is first, then month and you can compare like numbers
It should be
$today=strtotime("27-02-2015");
$end=strtotime("24-06-2015");
if($end < $today){
echo "yes";
}else{
echo "no";
}
Build Date objects from string specified in the way your locale settings mandate and compute the differences on epoch millisecond:
$s_today = '02/27/2015';
$s_end = '06/24/2015';
date_default_timezone_set ( 'UTC' ); # required by strtotime. Actual value doesn't matter as you are interested in the date diff only
$today = strtotime($s_today);
$end = strtotime($s_end);
if ($end < $today) {
echo "less ( $s_today < $s_end )";
} else {
echo "more ( $s_today > $s_end )";
}
Note
#n-dru's approach is viable too. It depends which date specs your code fragment must cope with and if you wish to perform more computations on the dates. In the latter case I'd opt in favor of normalization to the epoch seconds. Otherwise it proabably isn't worth the hassle.

php compare YYYY-mm-dd strings in php

I have run across php code the compares dates in YYYY-mm-dd format as strings. Does this work? It seems to work in simple cases, but I am not sure it makes sense to compare them as strings.
<?php
$today = '2013-02-11';
$start_date = '2013-02-11';
$end_date = '2013-02-12';
$on_promo = (($start_date <= $today) && ($end_date >= $today));
if ($on_promo)
{
echo 'ON promo';
}
else
{
echo 'OFF promo';
}
?>
You're soooooo close. Just use DateTime. It's perfect for this;
<?php
$today = new DateTime('2013-02-11');
$start_date = new DateTime('2013-02-11');
$end_date = new DateTime('2013-02-12');
$on_promo = (($start_date <= $today) && ($end_date >= $today));
if ($on_promo)
{
echo 'ON promo';
}
else
{
echo 'OFF promo';
}
?>
See it in action
When comparing strings in PHP using greater than or less than, it compares them in alphabetical order.
Alphabetically 2013-02-10 comes before 2013-02-13
If we have:
$date1 = '2013-02-10';
$date2 = '2013-02-13';
var_dump($date2 > $date1); // produces true
var_dump('apple' > 'banana'); // produces false
However, note that if the strings are both numerical, it will cast them both to integers
var_dump('11' > '101'); // produces false
var_dump('a11' > 'a101'); // produces true
var_dump('11a' > '101a'); // produces true
Therefore if using the format YYYY-MM-DD you can compare two dates perfectly fine, however I really don't recommend relying on this. Someone might throw in a date like 2013-2-11 (note the month doesn't have the leading 0) and it will completely throw off the logic. It is much better to take John Conde's suggestion and use DateTime
use strtotime instead of comparing dates as strings
<?php
$today = date('U');
$start_date = strtotime('2013-02-11');
$end_date = strtotime('2013-02-12');
$on_promo = (($start_date <= $today) && ($end_date >= $today));
if ($on_promo)
{
echo 'ON promo';
}
else
{
echo 'OFF promo';
}
?>

Categories