-1

I have two date objects say 10/01/2016 00:00:00 to 18/01/2016 08:00:00. I want to check whether these 2 dates lies between friday 12 AM to Sunday 12 Am then how to find difference in hours ?

PHP code -

$t1 = StrToTime($date2);
$t2 = StrToTime($date1);
$diff = $t1-$t2;

$hours = abs($diff / ( 60 * 60 ));

I am badly stuck in this. Please someone help.

Thanks in Advance.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

2 Answers2

0

If I understand your question correctly you want to test if the date is either a friday or a saturday, with no relevance how far they are away from each other.

You can use the format() function of the DateTime object to get the weekday of the date, then test if it is a friday (5) or a saturday (6):

$t1 = DateTime::createFromFormat("d/m/Y H:i:s", "10/01/2016 00:00:00");
$t2 = DateTime::createFromFormat("d/m/Y H:i:s", "18/01/2016 08:00:00");

function testDate($date) {
    $d = $date->format("N");
    if ($d == 5) return true;
    if ($d == 6) return true;
    return false;
}

$d1 = testDate($t1);
$d2 = testDate($t2);

var_dump($d1, $d2);

// Result:
// bool(false)
// bool(false)

Then you can use the DateTime::diff() function to get the difference:

$diff = $t1->diff($t2);
var_dump($diff);

Result:

object(DateInterval)#3 (15) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(8)
  ["h"]=>
  int(8)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(8)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

So, 8 days ($diff['days']) and 8 hours ($diff['h'])

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
0

You can use:

$date1 = "2016-08-14 00:00:00"; //more higger day
$date2 = "2016-08-13 00:00:00";    
$diff = abs(strtotime($date2) - strtotime($date1)); 

$years   = floor($diff / (365*60*60*24)); 
$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));    
$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));     
$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);     
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 

printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds); 

Output: 0 years, 0 months, 1 days, 0 hours, 0 minuts , 0 seconds

Now, you can do:

$date1 = "2016-08-14 00:00:00"; //more higger day
$date2 = "2016-08-13 00:00:00";    
$diff = abs(strtotime($date2) - strtotime($date1)); 

$years   = floor($diff / (365*60*60*24)); 
$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));    
$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));   

echo "Totals hours: ".($days*24 + $hours);

Returns 24 (and you can multiply the years, months and so on)

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39