I want to count the total day difference from user input
For example when the user inputs
start_date = 2012-09-06
and end-date = 2012-09-11
For now I am using this code to find the diffeence
$count = abs(strtotime($start_date) - strtotime($end_date));
$day = $count+86400;
$total = floor($day/(60*60*24));
The result of total will be 6. But the problem is that I dont want to include the days at weekend (Saturday and Sunday)
2012-09-06
2012-09-07
2012-09-08 Saturday
2012-09-09 Sunday
2012-09-10
2012-09-11
So the result will be 4
----update---
I have a table that contains date,the table name is holiday date
for example the table contains 2012-09-07
So, the total day will be 3, because it didn't count the holiday date
how do I do that to equate the date from input to date in table?
If you don't need full days but accurate seconds instead try this code. This accepts unix timestamps as an input.
Here's an alternative to calculate business days between two dates and also excludes USA holidays using Pear's Date_Holidays from http://pear.php.net/package/Date_Holidays.
$start_date and $end_date should be DateTime objects (you can use
new DateTime('@'.$timestamp)
to convert from timestamp to DateTime object).Very easy with my favourites:
DateTime
,DateInterval
andDatePeriod
date('N') gets the day of the week (1 - Monday, 7 - Sunday)
The easiest and fastest way to get difference without weekends is by using Carbon library.
Here's an example how to use it:
In my case I needed the same answer as OP, but wanted something a little smaller. @Bojan's answer worked, but I didn't like that it doesn't work with
DateTime
objects, required using timestamps, and was comparing againststrings
instead of the actual objects themselves (which feels hacky)... Here's a revised version of his answer.Per @xzdead's comment if you'd like this to be inclusive of the start and end date: