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.'
Related
This question already has answers here:
Carbon Difference in Time between two Dates in hh:mm:ss format
(5 answers)
Closed 3 years ago.
I would like to find total time spent on a project given that the user enters the "start time" and "stop time". I have been able to access the time spent but it comes as an array of Date Interval.
I want to just format the results to "H:i:s" (Hour:Minute:Seconds)
Here's the code from my controller
I have already declare the use of Carbon Class(use Carbon/Carbon;) at the top of the controller
$start = Carbon::parse($request->strt_time);
$end = Carbon::parse($request->stp_time);
$time_spent = $end->diff($start);
$spent_time = $time_spent->format('H:i:s');
I expect the output to be 00:00:00 but I am getting a string "H:i:s"
From the Carbon documentation:
Difference
As Carbon extends DateTime it inherit its methods such as diff()
that take a second date object as argument and returns a
DateInterval instance.
We also provide diffAsCarbonInterval() act like diff() but returns
a CarbonInterval instance. Check CarbonInterval chapter for more
information.
So, as Akash suggested, you could do:
$spent_time = $end->diff($start)->format('%H:%i:%s');
Why the % is prefixed in every variable? As #aynber pointed out, the documentation states:
Each format character must be prefixed by a percent sign (%).
Another option is to make use of the gmdate() helper:
$duration = $end->diffInSeconds($start);
$spent_time = gmdate('H:i:s', $duration);
or just:
$spent_time = gmdate('H:i:s', $end->diffInSeconds($start));
diff() method gives CarbonInterval, which inherits the format function from DateInterval. The documentation states that Each format character must be prefixed by a percent sign (%)
DateInterval::format ( string $format ) : string
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
so final solution is
$end->diff($start)->format('%H:%i:%s');
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";
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 5 years ago.
I have this php code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
date_diff() needs a DateTimeInterface as an argument. In other words, you need to create a DateTime object first, using new DateTime() as shown below.
$today_date = new DateTime();
$Expierdate = new DateTime('09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Live demo
The above would output
90 days
Because today is June 8th, and the format 09/06/2017 is September 6th - because you're using American format (MM/DD/YYYY).
If you ment June 9th (tomorrow), you need to use European format (MM-DD-YYYY, note the dash instead of slash). You can alternatively use DateTime::createFromFormat() to create from a set format, so your current format, 09/06/2017, is interpreted as June 9th. The code would then be
$today_date = new DateTime();
$Expierdate = DateTime::createFromFormat('d/m/Y H:i:s', '09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Output (live demo)
1 days
In any case, $remaindate holds some properties which can be used (see the manual), or you can format it to your liking by supplying the desired formation into the format() method.
new DateTime()
DateTime::diff()
DateTime::format()
DateTime::create_from_format()
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.
This question already has answers here:
PHP remove dots and hyphens
(5 answers)
Closed 3 years ago.
I want to convert date from, for example, 1441065600 to YYYYMMDD format. How do I do that?
$temp = date("Y-m-d",1441065600);
$rel_date = date_format( $temp, "YYYYMMDD");
The code above gives an error:
date_format() expects parameter 1 to be DateTimeInterface
Using date()
Simply write:
<?php
echo date('Ymd', 1441065600);
Otherwise, using a DateTime instance:
Short
<?php
echo (new DateTime())->setTimestamp(1441065600)->format('Ymd');
Note: the setTimestamp() method do not takes a string as parameter! Otherwise, you may want to do so:
Long
<?php
$english = '1441065600';
$timestamp = strtotime($english);
$date = new DateTime($timestamp);
echo $date->format('Ymd'); // 20161214
Descriptions
strtotime() - Parse about any English textual datetime description into a Unix timestamp
DateTime - Representation of date and time
Note: I created a new DateTime instance from the UNIX timestamp, English textual representation may lead to an error.
Other formats
I recommend you to read the DateTime::format() method documentation along with the date() function documentation to learn more about date formats.
$date = (new DateTime())->setTimestamp(1441065600);
echo $date->format('Ymd');
date_format is function in which first argument should be DateTime object, date function return string.
So first You need to create correct object.
$date = new DateTime(strtotime(1441065600));
and then format it with date_format
echo date_format($date,'Ydm');
You can pass the timestamp to the DateTime constructor by prepending # character:
$d = new DateTime('#1441065600');
echo $d->format("Ymd");
See Compound Formats.
Use Date function in PHP
echo $temp = date("Y-m-d",1441065600);
Result:
2015-09-01
Refer http://php.net/manual/en/function.date.php