How to display total number of days between two dates using php - php

I want to count the total number of days between two date.
Note: I am not talking about the difference between two date
I found lots of answer on google and Stack Overflow. I am sharing here
$now = time(); // or your date as well
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
Output is 9
$now = strtotime("2018-06-09");
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo $numberDays= round($datediff / (60 * 60 * 24));
Output is 8
$dStart = new DateTime('2018-06-01');
$dEnd = new DateTime('2018-06-09');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->days;
Output is 8
$date1=date_create("2018-06-01");
$date2=date_create("2018-06-09");
$diff=date_diff($date1,$date2);
echo $diff->format("%a");
Output is 8
$startTimeStamp = strtotime("2018-06-01");
$endTimeStamp = strtotime("2018-06-09");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
echo $numberDays = intval($numberDays);
Output is 8
The correct answer for me is the first one 9 because it displays the total number of days from 1 to 9, not a difference.
I just want to know how to get the count because I don't want to use the time() because it's taking the current date.
For example: If I select the first date 2018-05-01 and my second date in 2018-05-31. So total days is 31, not 30.
Example:
I am working on a project which is related to the working days and salary. So employee will select the date. for example. I am the employee and in the last month May, I select the date 2018-05-01 and 2018-05-31 so I have to display the total working days.So the according to the date I worked 31 days and using above code in the question I am getting 30 days.
Hope you understand my issue. Would you help me out in this?

You can try this-
$dateOne = new DateTime("01-01-2018");
$dateTwo = new DateTime("09-06-2018");
echo $diff = $dateTwo->diff($dateOne)->format("%a");
Output- 159

Use the DateTime class and compare them via diff() method. $date1->diff($date2). The output will give you an accurate result of days:
$date1 = new DateTime('2018-01-01');
$date2 = new DateTime('2018-02-15');
print_r($date1->diff($date2));
Output:
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 14
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 45
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output : 272 days.

You can try this:
<?php
$start_date = "2020-12-24";
$end_date = "2020-12-31";
$dateDiff = strtotime($end_date) - strtotime($start_date);
$numOfDays = $dateDiff / 86400;
echo $numOfDays;
?>
Output: 7

First compile and print $now and write down out put after some minutes 2nd time compile and print $now you will ge a difference

here i got your solution in first program php time(); function is dynamically change as per current time in millisecond (In PHP you can simply call time() to get the time passed since January 1 1970 00:00:00 GMT in seconds) here you are using round function to get rounded value after 12pm it your calculation will increase more then 8.5 and you get a rounded value which is 9
here brief explanation Getting unix timestamp in milliseconds in PHP5 and Actionscript3
in rest of program you are taking static time or custom time which are not effected by system time
$now = time(); // or your date as well
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
try above program before 12pm maybe you got your answer

Related

How to add an odd day to a time in PHP?

How to add an odd day to a time in PHP?
Like add 1.5 day? Any idea?
When I try like 1.5 or 1,5 then it's adding 15 days.
Is there anyway to add odd day to time?
If there are fractions of days, they can be converted to seconds and then added.
$days = 1.5;
$seconds = (int)(86400 * $days);
$dt = new Datetime('2022-04-01');
$dt->modify($seconds.' Seconds'); //add
echo $dt->format('Y-m-d H:i');
//2022-04-02 12:00
Also works with $days = 1.25; This then returns 2022-04-02 06:00 as a result.
Or $days = 1.1 returns 2022-04-02 02:24.
Following is the one approach you can do first add an exact number of days(non-fractional part) and then add fractional value in hours.
For example, If you want to add 1.5 days then add 1 day and 12 hours.
echo date("Y-m-d H:i:s", strtotime('+1 Day +12 Hours'))

Days calculation is not given correct days

I would like to calculate days based on two dates but it's given incorrect count of days.
I have 2 variables
$date=$date[1]; Is coming from Database
$now =date('d/m/Y'); Current date
$date variable value is(comes from db) 05/03/2019
I used following script for count days based on two dates but its return 120 days.
Method 1
$date=$date[1]; `Is coming from Database`
$now =date('d/m/Y');
$date1 = new DateTime($now);
$date2 = new DateTime($date);
$diff = $date2->diff($date1)->format("%a");
Method 2
$datetime1 = new DateTime($now);
$datetime2 = new DateTime($date);
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
print_r($difference);
But it's returns wrong days in following output
Difference: 0 years, 4 months, 120 daysDateInterval Object
(
[y] = 0
[m] = 4
[d] = 0
[h] = 0
[i] = 0
[s] = 0
[invert] = 0
[days] = 120
)
Why this given wrong output of days ?
If you use 'd/m/Y' format you should use createFromFormat function to convert string date to date object. For example:
$date1 = new \DateTime();
$date2 = \DateTime::createFromFormat('d/m/Y', '05/03/2019');
$diff = $date2->diff($date1)->format("%a");

How to get the difference between two dates with 360 days/year, 30 days/month format?

I can get the difference between two dates (DD/MM/YY format) with the following code;
$date1 = new DateTime("1986-04-27");
$today = new DateTime("now");
$interval = $date1->diff($today);
$year = $interval->format('%y');
$month = $interval->format('%m');
$day = $interval->format('%d');
It works perfect when calculating 1 year 2 months but when I'm trying to calculate 1 year 2 months 3 days, it doesn't give me the accurate result.
I would like to get the difference between two dates, using 360 days for a year, 30 days for a month, without calculating leap years..
For example; if a month has 31 days the system will calculate it as 30 days.
If a year has 365 days, the system will calculate it as 1 year 5 days. (360 + 5)
How can I do that?
Thank you..
Here is the way to do it:
function diff360($date1, $date2) {
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$diff = $date1->diff($date2);
$days = ($date2->format('d') + 30 - $date1->format('d')) % 30;
return array(
"y" => $diff->y,
"m" => $diff->m,
"d" => $days,
"totaldays" => $diff->y * 360 + $diff->m * 30 + $days
);
}
$result = diff360("2016-02-06", "2016-03-06");
var_export($result);
Output:
array (
'y' => 0,
'm' => 1,
'd' => 0,
'totaldays' => 30,
)
Just get the number of days and substract the years / months.
$totalDays = $interval->format('%a');
$years = 0;
$months = 0;
while ($totaldays >= 360) { $years++; $totaldays -=360; }
while ($totaldays >= 30) { $months++; $totaldays -=30; }
$days = $totalDays
But why do you need a broken date format?
The DateTime class will calculate a difference based on real dates. It's kind of random whether it will work or not in your example. If you want to use custom lengths for months, ignore leap years etc. then you will have to create custom functions yourself.
I don't really understand the point of doing this though?

Make a timer reset every 30 days?

I currently have code that changes the month number and MYSQL table every month automatically but the timer it displays still resets every 24 hours. I need to make it so the timer resets every month instead of every 24 hours. I am not thinking straight and need some help solving this.
Basically I need it so that $month_end_time counts down from 30 days, 0 hours, 0 minutes 0 seconds down to 0 days, 0 hours, 0 minutes 0 seconds and then resets back to the 30 days. Currently it counts down from 30 days to 29 days then resets as it is from a script that resets every 24 hours and I am porting it to monthly.
Credits to #ElmoVanKielmo for the original snippet.
Thanks in advance.
define("FIRST_DAY_STRING", "2014-4-6");
define("SHIFT_DAYS", 'P30D');
define("TIME_SUFFIX", " 0:00: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));
$month_number = floor(intval($days) / 30 + 1);
$txid = "tx$month_number";
$month_end_time = $end_date->format('Y-n-j');
$month_end_time .= TIME_SUFFIX;
I suspect you're possibly overthinking this, since it includes "dates". When moving around months, it can be tricky, since (as Raptor notes), months have differing number of days between each other.
However, based on your comments, you're actually looking for the number of 30-day periods between one date and another. This can be accomplished with basic math and Unix timestamps:
$start = strtotime('2012-04-12 00:00:00 GMT');
$today = strtotime('00:00:00 GMT');
$days = ($today - $start) / 60 / 60 / 24;
$months = $days / 30;
echo "<pre>
Days: $days
Months: $months
";
This will give:
Days: 723
Months: 24.1
http://codepad.viper-7.com/KBVfqq
And if you're trying to figure out how many days:
$start = strtotime('2012-04-12 00:00:00 GMT');
$today = strtotime('00:00:00 GMT');
$days = ($today - $start) / 60 / 60 / 24;
$months = $days / 30;
$months_days = floor($months) . " months, " . ($days - (floor($months) * 30)) . " days";
echo "<pre>
Days: $days
Months: $months
Months and Days: $months_days
";
Giving:
Days: 723
Months: 24.1
Months and Days: 24 months, 3 days
http://codepad.viper-7.com/AdnFsu
Which means that between the start and today's date, there have been 24 full 30-day periods, and we are currently in the 25th period (ceil($months)). This seems sufficient for what you are after, although the specific use of the period value may require better explanation.

Simple strtotime math

I'm trying to get the total minutes and seconds that have elapsed from the following step by step:
1: strtotime('now') - strtotime('-1 day');
2: Ill get something back like 120 lets say...
3: So... 120 = 2 minutes -- My problem is the remainder though!
4: Examples:
130 = 2 minutes 10 seconds
130 / 60 = 2.1666~
121 = 2 minutes 1 seconds
121 / 60 = 2.0166~
122 = 2 minutes 2 seconds
122 / 60 = 2.0366~
5: So using the example 122, I have 2 minutes,
but how to I grab the 2 seconds from there?
I was trying to explode on the period,
and use the remainder, but that's not right.
PS: I never had a gift for Math.
You can also use DateTime math (php >= 5.3):
$d1 = new DateTime(strtotime('now'));
$d2 = new DateTime(strtotime('-1 days'));
$diff = $d1->diff($d2);
echo "{$diff->i} minutes, {$diff->s} seconds\n";
This is how you can use PHP's DateTime::diff which returns DateInterval
$dt = new DateTime();
$dt2 = new DateTime("-120 seconds");
$diff = $dt->diff( $dt2 );
echo( $diff->format("%i min and %s sec") );

Categories