DateTime::diff returns unexpected result - php

I have the following code that is returning an unexpected answer. Please let me know what's wrong.
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";
It is returning the following output:
day: 0 month: 1
I expect the output to be:
day: 1 month: 1

This will give you one day and one month https://3v4l.org/q0T8r
$start_date = new DateTime('31-03-2019 00:00:00');
$end_date = new DateTime('01-05-2019 24:00:00');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";

When you add 1 month to 2019-03-31, PHP will internally just increment the month value 03 to 04. The result is 2019-04-31.
As April has only 30 days, 2019-04-31 has the same meaning as 2019-05-01 has. And that's the reason, why you get one month and zero days as the result.
The DateInterval class has another handy property: days instead of m and d. It will contain the total number of days between the two dates, which equals to 31 (you have to add 31 days to 2019-03-31 to get to the 2019-05-01.
On this value you can implement your own logic, what "one month" is. If you define it as "one month = 30 days", this could be your whished result:
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$diff = $start_date->diff($end_date);
$months = floor($diff->days / 30);
$days = $diff->days % 30;
echo "day: " . $days . " month: " . $months . "\n";

Related

Difference Date Calculation

i've problem with calculating difference between two dates (including the End Date) using Carbon. Here's the problem:
I'm using this code (source: danharper's answer in https://laracasts.com/discuss/channels/general-discussion/carbon-display-age-in-years-months-day?page=1) :
$dateFrom = new Carbon("2017-01-01");
$dateTo = new Carbon("2017-12-31");
$dateTo = $dateTo->addDay(); //including End Date
echo $dateFrom->diff($dateTo)->format('%y') . " year, <br>";
echo $dateFrom->diff($dateTo)->format('%m') . " month, <br>";
echo $dateFrom->diff($dateTo)->format('%d') . " day <br>";
echo "difference " . $dateFrom->diffInDays($dateTo) . " days <br>";
Scenario 1:
Let's say, $date1 = 2017-01-01 and $date2 = 2017-12-31, then it'll results:
1 year, 0 month, 0 day
difference 365 days
When i'm using date calculator in https://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=2017&d2=31&m2=12&y2=2017&ti=on, it'll results:
It is 365 days from the start date to the end date, end date included
Or 1 year including the end date
They resulting the same answer. BUT:
Scenario 2:
$date1 = 2017-10-01 and $date2 = 2017-12-31, then it'll results:
0 year, 3 month, 1 day
difference 92 days
Using date calculator in https://www.timeanddate.com/date/durationresult.html?d1=1&m1=10&y1=2017&d2=31&m2=12&y2=2017&ti=on, it'll results:
It is 92 days from the start date to the end date, end date included
Or 3 months including the end date
The result in timeanddate.com is exactly 3 months ONLY. Not with 1 day.
I want the result is 3 months (the timeanddate.com's answer).
How can i achieve that answer?
Or, if it can't be achieved, is there any other technique to achieve:
x months y days? (ex: 1 jan 2017 ~ 5 feb 2019 = 25 months, 5 days)
Please help me.
You can change you code like following,
$dateFrom = new Carbon("2017-01-01");
$dateTo = new Carbon("2017-12-31");
$dateTo = $dateTo->addDay(); //including End Date
$days = $dateFrom->diffInDays($dateTo);
$months = $dateFrom->diffInMonths($dateTo);
$years = $dateFrom->diffInYears($dateto);
In you code, you have measured difference many times instead of one time. Use diffInDays(), diffInMonths() and diffInYears() functions to get values of days, months and years between two dates.
Hope you understand.
Scenario 1
$start_date = new DateTime('1 Jan 2017');
$end_date = new DateTime('5 Feb 2019 +1 day');
$difference = $start_date->diff($end_date);
$year_diff = $difference->format('%y');
$months_diff = $difference->format('%m');
$total_months = $months_diff + ($year_diff * 12);
$output = $total_months . ' months ' . $difference->format('%d') . ' days';
// would output 25 months 5 days
Scenario 2
$start_date = new DateTime('2017-10-01');
$end_date = new DateTime('2017-12-31 +1 day');
$difference = $start_date->diff($end_date);
$year_diff = $difference->format('%y');
$months_diff = $difference->format('%m');
$total_months = $months_diff + ($year_diff * 12);
$output = $total_months . ' months ' . $difference->format('%d') . ' days';
// would output 3 months 1 days

Getting the Date and numeric weekday in PHP

i'm developing an application in PHP and I need to use dates and the numeric representation of weekdays.
I've tried the following:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', strtotime($tomorrow));
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
Output
Today: 2016-11-11 weekday: 5
Tomorrow: 2016-11-12 weekday: 4
This isn't right because the weekday of tomorrow should be 6 instead of 4.
can someone help me out?
Using DateTime would provide a simple solution
<?php
$date = new DateTime();
echo 'Today: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
$date->modify( '+1 days' );
echo 'Tomorrow: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
Output
Today: 2016-11-11 weekday 5
Tomorrow: 2016-11-12 weekday 6
However the day numbers are slightly different, the N respresents the weekday number and as you can see Friday (Today) is shown as 5. With that Monday would be 1 and Sunday would be 7.
If you look at the example below you should get the same result
echo date( 'N' );
Output
5
Date Formatting - http://php.net/manual/en/function.date.php
You have little error in the code, here`s the working one:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', $tomorrow);
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
DateTime is the Object Oriented method of working with dates in PHP. I find it to be working much more fluently. That aside, it looks alot better.
// Create a new instance
$now = new DateTime();
echo $now->format('N');
// Next day
$now->modify('+1 day');
echo $now->format('N');
Resources
DateTime manual - PHP.net
You almost have it right but not quite. Why are you using strtotime on $number2? Change it to $number2 = date('N', $tomorrow); and it will work.

user defined months added to given date

I am trying to add a user defined amount of months to the date previously added. $CustDate is already in YYYY-MM-DD format from previous form.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d=strtotime("+" . $months . " Months");
$CustAddedDate=date("Y-m-d", strtotime($CustDate, $d));
If I enter the date as: 2016-08-04 as the $CustDate, it gives me the same value for the $CustAddedDate.
Where am I screwing this up? Thanks!
You add the + months along with the $CustDate. Provide the $Cusdate as the second argument in the addition.
$CustAddedDate = date('Y-m-d', strtotime("+" . $months . " Months", strtotime($CustDate)));
// ^ add this with the addition
Or the DateTime variant:
$date = new DateTime($CustDate);
$date->modify('+ ' . $months . ' Month');
$CustAddedDate = $date->format('Y-m-d');
echo $CustAddedDate;
Note:
$d = strtotime('2016-03-02'); // March 2nd, 2016 -> 1456898400
echo date('Y-m-d', strtotime('+1 day', $d)); -> 2016-03-03
echo date('Y-m-d', strtotime('2010-01-02', $d)); -> 2010-01-02
The second argument for strtotime() set a time basis for any "relative" time values, like +1 day or yesterday. Since you're passing in an absolute date, 2016-08-04, there's no "relative" measure to base anything on, and your absolute date is used in its entirety for the conversion.
If you want to adjust that absolute date, you have to do something like
echo date('Y-m-d', strtotime('2016-08-04 + 1 day')) -> 2016-08-05
e.g. embed the date math into the string you're passing into strtotime, and not in the second argument.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d="+" . $months . " Months"; //not strtotime time here!
$CustAddedDate=date("Y-m-d", strtotime($d,strtotime($CustDate)));//watch the order of arguments and missing strtotime of the existing date

Get the year, month week, day, hour, minute, second interval on two dates in PHP

How can I get the year, month week day, hour, minute, second interval on two dates on php?
For example:
date1 = 2015-9-24
date2 = 2015-12-25
Output:
3 months and 1 day left
Thanks guys.
You are use PHP date_diff() Function
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
Please try php diff
$date1 = new DateTime("2015-9-24");
$date2 = new DateTime("2015-12-25");
$interval = $date1->diff($date2);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
echo "Result " . $interval->days . " days ";
Try this
$a1 = new DateTime('2015-9-24');
$a2 = new DateTime('2015-12-25');
$interval = $a2->diff($a1);
$interval->format('%m months');

if current_time is 1 day over logged_time php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'

Categories