PHP DateTime diff issue between dates - php

$start = new DateTime('2014-01-07', new DateTimeZone('UTC'));
$end = clone $start;
$start->sub(new DateInterval('P1M')); // substract one month
echo $start->format('Y-m-d').' - '.$end->format('Y-m-d');
// 2013-12-07 - 2014-01-07 - seems correct, let's get number of days...
print_r($end->diff($start));
// DateInterval Object ( [y] => 0 [m] => 2 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [invert] => 1 [days] => 61 )
61 days??? What is going on in here?

try this,
$diff=date_diff($date1,$date2);

Related

DateTime diff returns me 0 when the compared DateTimes are different

I would like to return the hours and minutes of difference between two dates. The problem is that the result of the diff method returns me a diff of 0 (same DateTimes).
Here is my actual code :
$last_vote_date = new DateTime(\Auth::user()->last_vote_at);
$next_vote_date = $last_vote_date;
$next_vote_date->add(new DateInterval('PT3H'));
$diff = $next_vote_date->diff($last_vote_date, true);
$vote_hours = $diff->format('%h h %I m');
I debugged my vars. $last_vote_date is the correct DateTime.
$next_vote_date is a correct DateTime representing the last vote date + 3 hours.
However all the properties of the DateInterval object that diff returns are all 0 :
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
I really don't understand what's going wrong as my two DateTime objects being compared are different and are exactly the value I want.
When you do $next_vote_date = $last_vote_date you are basically copying a reference. Meaning any changes you make to $next_vote_date will affect $last_vote_date. The quick way to deal with this is to copy the object:
$last_vote_date = new DateTime(\Auth::user()->last_vote_at);
$next_vote_date = clone $last_vote_date;
$next_vote_date->add(new DateInterval('PT3H'));
$diff = $next_vote_date->diff($last_vote_date, true);
$vote_hours = $diff->format('%h h %I m');

How to show server run time in laravel?

i want to show date to user from how much days website is running
e.g today : runing from 10 days . next day : running from 11 days
$current = time();
$initial_date = strtotime("2017-11-1"); //You will have to fix this
$datediff = $current - $initial_date;
$num_of_days = floor($datediff / (60 * 60 * 24));
echo "Running from ".$num_of_days." days";
Note: This will not count today.
It's not laravel specific, you can use this in PHP generally:
exec("uptime", $uptimeVar);
echo($uptimeVar[0]);
You can also format it any way you like.
This way helpful to you. first define the web site starting date as a constant as follow:
define('START_DATE', '2017-11-02');
Then you have to put below code in to the place you have to show the date difference:
$date1 = new DateTime(START_DATE);
$date2 = new DateTime(date('Y-m-d'));
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
echo $diff->days . " day(s)"; // output '6 day(s)'
Output looks like:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 6
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 6
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
6 day(s)

php minus past date from current date and get result in days [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
Im looking for the less complicated way to do the following:
$joinDate = "2014-05-26"
$date = date('Ymd'); //todays date
$memberFor = $joinDate - $date //This is where I need to get total number of days
Is there a function that can help me with this?
You should be using DateTime Object for these operations
$joinDate = "2014-05-26";
$joinDate_obj = new DateTime($joinDate);
$now = new DateTime();
$interval = $joinDate_obj->diff($now);
$diff = $interval->d ;
echo $diff; //12
The object $interval will have
DateInterval Object (
[y] => 0
[m] => 11
[d] => 12
[h] => 12
[i] => 49
[s] => 4
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 347
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0 [have_special_relative] => 0
)
So you may use for example $interval->days for the difference in days.

How to get current date/time as a date object in PHP

How do you get today's date, as a date object?
I'm trying to compute the difference between some start date and today. The following will not work, because getdate() returns an array and not a date object:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
Output:
Array ( [seconds] => 8 [minutes] => 1 [hours] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [year] => 2012 [yday] => 162 [weekday] => Monday [month] => June [0] => 1339455668 )
DateTime Object ( [date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America/Los_Angeles )
new DateTime('now');
http://www.php.net/manual/en/datetime.construct.php
Comparing is easy:
$today = new DateTime('now');
$newYear = new DateTime('2012-01-01');
if ($today > $newYear) {
}
Op's edit
I just needed to call date_default_timezone_set, and then this code worked for me.
To get difference in days use this:
$today = new DateTime('today');
the time in this object eill be 00:00:00
If you want difference with hours minutes and seconds use this:
$now = new DateTime('now');
I ended up using the date_create constructor (no parameter) to get the current date.
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.

Weird issue with PHP's DateTime::add not adding time

I've managed to reduce this to a small script which reproduces the issue (Tried on two different PC's, but both with 5.3.6):
<?php
$item = array('monthly_on_the' => 4);
$date = new DateTime();
$date->modify('first day of this month');
print_r($date);
$interval = new DateInterval('P'.$item['monthly_on_the'].'D');
print_r($interval);
$return = $date->add($interval);
if (!$return) die('Bad stuff happened!');
print_r($date);
DateTime Object
(
[date] => 2012-02-01 17:15:23
[timezone_type] => 3
[timezone] => Australia/Sydney
)
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 4
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] =>
)
DateTime Object
(
[date] => 2012-02-01 17:15:23
[timezone_type] => 3
[timezone] => Australia/Sydney
)
Is this a bug and if so, has it been fixed in more recent versions of PHP? Or is there something weird in the time stuff that I'm overlooking?
Yes it's a bug. I'm experiencing the same thing with PHP 5.3.6. Apparently it's fixed in 5.3.7. See the changelog:
http://www.php.net/ChangeLog-5.php#5.3.7
Bug Report:
https://bugs.php.net/bug.php?id=54340
Workaround (if you can't upgrade):
$date->setTimestamp(strtotime('first day of this month', $date->getTimestamp()));

Categories