DateTime comparison not working PHP [duplicate] - php

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 6 years ago.
timing comparison not working out if passed dynamically
$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/New_York'));
$myTime =$date->format('H:i');
$myDay =$date->format('l');
if(($s<=$myTime) && ($e>=$myTime||$e=="00:00"))
{$open =1;}
Following works if hard coded
if(("11:30"<="05:39")&&("23:00">="05:39"||"23:00"=="00:00"))
{$open =1;}
where m i going wrong

// your first date
$dateA = '2008-03-01 13:34';
// your second date
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// something here
}

first of all you are using strings to compare time. Convert to timesstamp using strtotime (http://php.net/manual/en/function.strtotime.php) or mktime(http://php.net/manual/en/function.mktime.php) or use DateTime class (http://php.net/manual/en/class.datetime.php). When comparing strings they are not compared as time, but as character sequences.

Related

Calculating time passed since a textual date/time value [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I have a database from which I extract this date/time value: 2018-01-19 09:50:54
I want to print how many hours and minutes passed since that date, with regard to current server time:
I try this code:
$prev_date = "2018-01-19 09:50:54" // extracted from DB
$date_now = date("Y-m-d H:i:s"); // 24-hour date-time, matches DB format
$interval = $prev_date->diff($date_now); // I saw this on another thread
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
I get:
Fatal error: Call to a member function diff() on a non-object (on the
$interval=.... line )
I guess it is some kind of formatting problem, how do I fix that?
$prev_date is not a object. You need to transform to a DateTime object to use diff. Also, date() will return a string, you must use a DateTime object for this to be able to use diff function.
$prev_date = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-19 09:50:54');
$date_now = new DateTime(); // 24-hour date-time, matches DB format
$interval = $prev_date->diff($date_now); // I saw this on another thread
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";

Can date() be compared like this? [duplicate]

This question already has answers here:
Trying to define my own time type in PHP
(2 answers)
Closed 6 years ago.
I have had some issues with comparing date/times.
I had a working code that just stopped working all of a sudden. Now I made some changes to it and by some reason UNIX time is not correct.
Utime and nexttime is just an example string now, but in the code Utime is last time updated, and nexttime is next time a update will happen calculated from Utime.
Example:
$Utime = "201603300450"; // YmdHi 2016 03 30 04:50
$nexttime = "201603300520";
if (date("YmdHi")>=$Utime && date("YmdHi")>=$nexttime)
Is this a safe way to compare the timevalue? I had some issues with strtotime thats why I ask.
You should convert your dates to DateTime objects:
<?php
$Utime = "201603300450";
$nexttime = "201603300520";
$dateUtime = DateTime::createFromFormat('YmdHi', $Utime);
$datenexttime = DateTime::createFromFormat('YmdHi', $nexttime);
$nowDate = DateTime::createFromFormat('YmdHi', date('YmdHi'));
if ($nowDate>=$dateUtime && $nowDate>=$datenexttime)
//do your stuff...
Strtotime should work just fine, but you should stick to a valid format. "Y-m-d H:i:s" is a valid format for strtotime.
Hope it helps :)
strtotime() is a great helper in this regard.
Check out strtotime
$Utime = "201603300450";
$nexttime = "201603300520";
if ( strtotime('now') >= strtotime($Utime) && strtotime('now') >= strtotime($nexttime) )

compare two DateTime Objects [duplicate]

This question already has answers here:
How do I compare two DateTime objects in PHP 5.2.8?
(8 answers)
Closed 9 years ago.
actually I have 2 DateTime objects
$d1 = new DateTime('04/14/2013 8.00 AM');
$d2 = new DateTime('04/14/2013');
so without doing any changes to these 2 objects. is it possible equal these 2 objects.
To get the difference between two DateTime objects, use DateTime::diff() method:
$interval = $d1->diff($d2);
As a result, you will get a DateInterval object. To get formatted time difference from the DateInterval object, use format() method, for example:
echo $interval->format('%s seconds');
You can see more examples of comparing DateTime objects here.
Also check how to use format() method.
Yes you can use the built in diff method for the DateTime objects;
$interval = $d1->diff($2);
Heres the manual for it: DateTime::diff
Then to get just the days you can format the interval like this
$interval = $interval->format('%d');
You can format the dates to strip off the time like:
if ($d1->format('m/d/Y') == $d2->format('m/d/Y'))
echo 'equal';
else
echo 'not equal';
or like this:
if (date_format($d1, 'm/d/Y') == date_format($d2, 'm/d/Y'))
echo 'equal';
else
echo 'not equal';
The date_format will put the dates in this format: 04/14/2013
You have to do a date_format on $d2 because it's really '2013-04-14 00:00:00.'

Adding different times in php [duplicate]

This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
Hello just a simple question here.
Php's time related functions are confusing me ;-(
Given 2 variables
$start = "2013-07-25 20:24:13" ('Y-m-d H:i:s');
$duration = "0:55" ('H:i');
How may I add $duration to $start? should result in:
"2013-07-25 21:19:13"
Use DateTime() with DatePeriod()
$dt = new DateTime('2013-07-25 20:24:13');
$dt->add(new DatePeriod('P55M'));
echo $dt->format('Y-m-d H:i:s');

Add hours in a date in PHP [duplicate]

This question already has answers here:
Time calculation in php (add 10 hours)?
(7 answers)
Closed 9 years ago.
Want to add hours in a date .
Please help.
The PHP code is:
$createdDate=date_create("2013-03-17 07:11:00");
$hour = 4;
I tried using strtotime. But it gives an error.
Thanks
Use DateTime modify method.
$date = new DateTime("2013-03-17 07:11:00");
$date->modify("+4 hours");
echo $date->format("Y-m-d H:i:s");
DEMO.
You could use DateInterval:
$iv = new DateInterval("PT{$hour}H");
$createdDate->add($iv);
// $createdDate is now modified
You can use date_modify:
date_modify($createdDate, "+4 hours");

Categories