I would like to return the number of days between NOW and some datetime using DateTime object. My dates are:
$now = "2018-03-08 14:00:00";
$last = "2018-02-06 20:00:00";
And I do it like this:
$now = new DateTime();
$last_dt = new DateTime($last);
$difference = $last_dt->diff($now);
$difference->format('%d');
$num_of_days = $difference->d;
For some weird reason, the value of $num_of_days is 1 (instead of like 30)
Anybody knows why please?
Thank you
You need to use DateInterval::$days to get the total in days.
DateInterval::$d is just the number of days but in "grouping form", i.e. 32 days difference will return 2 for DateInterval::$d and 1 for DateInterval::$m.
$last = "2018-04-10 20:00:00";
$now = new DateTime();
$last_dt = new DateTime($last);
$difference = $last_dt->diff($now);
echo "Difference: ".$difference->m." months and ".$difference->d." days, or ".$difference->days." days in total";
Result
Difference: 1 months and 2 days, or 33 days in total
Demo
You can see more in the manual
Related
I have following code that add 2 days to a given date.
$myDate = 2018-07-28 11:00:00; // the date is picked from db
$penaltyDays = 2;
$date1 = new DateTime($myDate);
$date1->add(new DateInterval("P{$penaltyDays}D")); // add N days
$now = new DateTime();
$interval = $date1->diff($now); // get difference
$days = $interval->d; // difference in days
I want value of $days must be 0 after passing exactly 48 hours. If 3 days are passed the value of $days should be -1.
I will also appreciate if someone tell me efficient/proper way to get the result.
To make an efficient code according to your specification then I would rather use strtotime than DateTime.
This code checks if the current time is larger than the database time + two (or three) days in seconds.
$myDate = "2018-07-28 11:00:00";
$unix = strtotime($myDate);
if(time() > ($unix + 86400*3)){
$days = -1;
}else if(time() > ($unix + 86400*2)){
$days = 0;
}else{
$days = "something else";
}
Echo $days;
https://3v4l.org/d6Q15
How can I do the percent calculation with dates since I have the following code
$date_today = date("Y-m-d");
$date_db = $row['date_final'];
$date1 = new DateTime($date_today);
$date2 = new DateTime($date_db);
$diff = $date1->diff($date2);
$result = $diff->format("%a");
The result of my variable $result and the total days of difference of the given date1 and date2 as I do the percent with them with that result of the variable $result
Here's some psuedo code, which may or may not help.
$date_start = 20160101
$date_today = 20160315 // 75 days have passed
$date_end = 20161231 // 366 days in total
Calculate the difference between $date_start and $date_today, which should give you 75. I'll call that $date_so_far.
What you are missing as far as getting a percentage is the TOTAL number of days in the calculation. Get that by doing a difference between $date_end and $date_start. I'll call that $date_range.
$date_pct = $date_so_far / $date_range
// should give you roughly .21, which represents that you are about 20% into your range.
Is that what you are seeking?
my goal is to calculate the number of months between two dates including the starting month.
I am using this function to calculate.
function number_months($date1, $date2){
$begin = new DateTime(date('Y-m-d', strtotime($date1)));
$end = new DateTime(date('Y-m-d', strtotime($date2)));
$diff = $end->diff($begin);
return ($diff->format('%y') * 12) + $diff->format('%m') + 1;
}
In most cases in works fine, but when the function parameters are for example:
$date1 = 2015-11-04 00:00:00
$date2 = 2017-02-01 00:00:00
Function returns:
15
Should be 16. What am I missing? I did reseach here on Stackoverflow, have tried various implementations of provided code, but the problem still ocurs.
Thanks.
11-04 to 12-04 = 1 month.
12-04 to 01-04 = 1 month.
01-04 to 02-01 = 0 month.
Result diff->format('%m') = 2.
($diff->format('%y') * 12) + $diff->format('%m') + 1 = 1*12 + 2 + 1 = 15;
It's true.
The problem is that the user will set the start and end date of project. And I need to create a input for every month the project is going to be set. So i need number 16 in this case.
Thanks to comments i realised that DateTime::diff() works in full units when it comes to years, months and days.
I solved my problem by setting the start and end date to 1st of the month. So now my function returns number of months between two dates including starting and ending month.
function number_months($date1, $date2){
$begin = new DateTime(date('Y-m-d', strtotime($date1)));
$end = new DateTime(date('Y-m-d', strtotime($date2)));
$begin->modify('first day of this month')->setTime(12, 00);
$end->modify('first day of this month')->setTime(12, 00);
$diff = $end->diff($begin);
return ($diff->format('%y') * 12) + $diff->format('%m') + 1;
}
I'm trying to calculate the number of hours a person has worked on a given day. To do so, I need to get the difference between two DateTime objects in Hours, Minutes, and Seconds. So far I can successfully do so like this
$timeIn = new DateTime($time['timeIn']);
$timeOut = new DateTime($time['timeOut']);
$time['hours'] = date_diff($timeIn, $timeOut) -> format("%H:%i:%s");
This seems to work fine, until I put in a test case where the employee forgot to clock out. Now, let's say that
$timeIn = '2016-09-28 14:26:17'
$timeOut = '2016-09-30 09:53:53'
In that case, the difference SHOULD be 43:27:36 (Because there is over a day in between the timeIn and timeOut). Instead, I get 19:27:36 (as if it's just truncating the days off and returning the rest). How can I add that day onto the hours instead of truncating it? (I'm looking to get 43:27:36, NOT 1day, 19 hours, ect ect. So I'm trying to get the answer in HH:MM:SS)
As Scott suggested but with a tweak, we'll need to format this ourselves, but we have nifty sprintf to help:
$start = new \DateTime("2016-09-28 14:26:17");
$end = new \DateTime("2016-09-30 09:53:53");
$interval = $end->diff($start);
$time = sprintf(
'%d:%02d:%02d',
($interval->d * 24) + $interval->h,
$interval->i,
$interval->s
);
I prefer using the \DateTime objects - although there shouldn't be much of a difference in the end date_diff is just an alias of \DateTime::diff.
You want to check the number of days in the DateInterval object;
$start = new \DateTime("2016-09-28 14:26:17");
$end = new \DateTime("2016-09-30 09:53:53");
$interval = $end->diff($start);
$days = $interval->d;
if ($days > 0) {
echo $interval->format("%a %h:%i:%s\n");
} else {
echo $interval->format("%h:%i:%s\n");
}
date_diff() returns a DateInterval instance. Look at DateInterval::format method or DateInterval::days field. Hour, minute and second values refer to the remainder in the last day (if more than one). You are looking for a the total number of days.
$timeIn = new DateTime($time['timeIn']);
$timeOut = new DateTime($time['timeOut']);
$diff = date_diff($timeIn, $timeOut) -> format("%H:%i:%s");
$days = $diff->days;
$time['hours'] = // calculate based on days + remainder...
I am trying to group an index by weeks as opposed to days like the code currently does. My code for displaying the days since the start date (FIRST_DAY_STRING) and time on that day (TIME_SUFFIX) is below. I am having just a bit of trouble converting it to weeks since the start day instead of days.
Thanks for any help in advance. I would also appreciate an explanation of how to do it for months as well as I also need that.
<?php
define("FIRST_DAY_STRING", "2014-3-26");
define("SHIFT_DAYS", 'P2D');
define("TIME_SUFFIX", " 20:30:00 GMT+11:00");
$today = new DateTime();
$first_day = new DateTime(FIRST_DAY_STRING);
$interval = $first_day->diff($today);
$days = $interval->format('%R%a days');
$end_date = $today->add(new DateInterval(SHIFT_DAYS));
$day_number = intval($days) + 1;
$txid = "tx$day_number";
$end_time = $end_date->format('Y-n-j')
$end_time .= TIME_SUFFIX
?>
I did it myself. What I did was divide the inval($days) by 7 and rounded it to the nearest whole number:
$day_number = round(intval($days) / 7 + 1, 0);