php find a stack of time - php

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";

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

Compare dates in different years php [duplicate]

How can I compare two dates in PHP?
The date is stored in the database in the following format
2011-10-2
If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?
I tried this,
$today = date("Y-m-d");
$expire = $row->expireDate //from db
if($today < $expireDate) { //do something; }
but it doesn't really work that way. What's another way of doing it?
If all your dates are posterior to the 1st of January of 1970, you could use something like:
$today = date("Y-m-d");
$expire = $row->expireDate; //from database
$today_time = strtotime($today);
$expire_time = strtotime($expire);
if ($expire_time < $today_time) { /* do Something */ }
If you are using PHP 5 >= 5.2.0, you could use the DateTime class:
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($expire_dt < $today_dt) { /* Do something */ }
Or something along these lines.
in the database the date looks like this 2011-10-2
Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.
Just to compliment the already given answers, see the following example:
$today = new DateTime('');
$expireDate = new DateTime($row->expireDate); //from database
if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) {
//do something;
}
Update:
Or simple use old-school date() function:
if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
//echo not yet expired!
}
I would'nt do this with PHP.
A database should know, what day is today.( use MySQL->NOW() for example ), so it will be very easy to compare within the Query and return the result, without any problems depending on the used Date-Types
SELECT IF(expireDate < NOW(),TRUE,FALSE) as isExpired FROM tableName
$today = date('Y-m-d');//Y-m-d H:i:s
$expireDate = new DateTime($row->expireDate);// From db
$date1=date_create($today);
$date2=date_create($expireDate->format('Y-m-d'));
$diff=date_diff($date1,$date2);
//echo $timeDiff;
if($diff->days >= 30){
echo "Expired.";
}else{
echo "Not expired.";
}
Here's a way on how to get the difference between two dates in minutes.
// set dates
$date_compare1= date("d-m-Y h:i:s a", strtotime($date1));
// date now
$date_compare2= date("d-m-Y h:i:s a", strtotime($date2));
// calculate the difference
$difference = strtotime($date_compare1) - strtotime($date_compare2);
$difference_in_minutes = $difference / 60;
echo $difference_in_minutes;
You can convert the dates into UNIX timestamps and compare the difference between them in seconds.
$today_date=date("Y-m-d");
$entered_date=$_POST['date'];
$dateTimestamp1 = strtotime($today_date);
$dateTimestamp2 = strtotime($entered_date);
$diff= $dateTimestamp1-$dateTimestamp2;
//echo $diff;
if ($diff<=0)
{
echo "Enter a valid date";
}
I had that problem too and I solve it by:
$today = date("Ymd");
$expire = str_replace('-', '', $row->expireDate); //from db
if(($today - $expire) > $NUMBER_OF_DAYS)
{
//do something;
}
Here's my spin on how to get the difference in days between two dates with PHP.
Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.
$today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
$wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
$diff = $today->diff($wanted);
$days = $diff->days;
if (($diff->invert) != 0) $days = -1 * $days;
$overdue = (($days < 0) ? true : false);
print "<!-- (".(($days > 0) ? '+' : '').($days).") -->\n";
Found the answer on a blog and it's as simple as:
strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
And you'll get the days between the 2 dates.
This works because of PHP's string comparison logic. Simply you can check...
if ($startdate < $date) {// do something}
if ($startdate > $date) {// do something}
Both dates must be in the same format. Digits need to be zero-padded to the left and ordered from most significant to least significant. Y-m-d and Y-m-d H:i:s satisfy these conditions.
If you want a date ($date) to get expired in some interval for example a token expiration date when performing a password reset, here's how you can do:
$date = $row->expireDate;
$date->add(new DateInterval('PT24H')); // adds 24 hours
$now = new \DateTime();
if($now < $date) { /* expired after 24 hours */ }
But in your case you could do the comparison just as the following:
$today = new DateTime('Y-m-d');
$date = $row->expireDate;
if($today < $date) { /* do something */ }
first of all, try to give the format you want to the current date time of your server:
Obtain current date time
$current_date = getdate();
Separate date and time to manage them as you wish:
$current_date_only = $current_date[year].'-'.$current_date[mon].'-'.$current_date[mday];
$current_time_only = $current_date['hours'].':'.$current_date['minutes'].':'.$current_date['seconds'];
Compare it depending if you are using donly date or datetime in your DB:
$today = $current_date_only.' '.$current_time_only;
or
$today = $current_date_only;
if($today < $expireDate)
hope it helps

Php dates is not compare properly

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.

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.

Comparing DateTime Not Working

I created two datetime objects where $date1 = 09/02/2013 and $date2 = 03/02/2014
When I run the following code:
if ($date2 < $date1)
{
echo "hi";
}
for some reason it echos "hi" although $date2 is clearly greater than $date1. How am i supposed to compare these two dates? Please help!
<?php
$date1 = new DateTime ('2013-12-25');
$date2 = new DateTime ('2014-11-24');
if ($date1 > $date2) {
echo ('date1 is greater than date2');
}
else {
echo ('date2 is greater than date1');
}
use like below with function http://php.net/manual/en/function.strtotime.php
if (strtotime($date2 ) < strtotime($date1))
{
echo "hi";
}
hope this will sure help you.
That could work in JavaScript, but in PHP it will not :P
However, you could calculate an interval between dates.
$interval = $date1->diff($date2);
if ($interval->invert){ //1 if negative and 0 if positive
// $date2 has a bigger time value
} else {
// $date1 has a bigger time value
}

Categories