-1

I have two dates start and end that are already booked

<?php
$arr2 = range(strtotime($_POST['start']), strtotime($_POST['end']), "86400");
//$arr2 = range(strtotime($row['check_out_date']),strtotime($row['check_in_date']), "86400");
//echo $arr1; 
array_walk_recursive($arr2, function(&$element) {
  $element = date("j-n-Y", $element);
});
//array_walk_recursive($arr2, function(&$element) { $element = date("d-n-Y", $element); });
$es = array();
//array_push($events, $e);
$name = json_encode($es);
//echo $name;    
foreach ($arr2 as $values) {
  $arr4[] = $values;
  echo $values;
}
?>

in arr4[] i am getting the between dates that are already booked like b/w 1-3 all dates, same i am getting that user want to booked dates,

how to compare that two dates, and match if dates are matching between then show alert dates are matching, else perfom insert query, so please help.

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77

2 Answers2

0

There are several ways to compare dates in PHP, but the method I use is using PHP's built-in DateTime class:

<?php
$timezone = new DateTimezone('America/Chicago');
$start = DateTime::createFromFormat('Y-m-d H:i:s','2016-01-01 00:00:00',$timezone);
$end = DateTime::createFromFormat('Y-m-d H:i:s','2016-02-02 00:00:00',$timezone);
// PHP 5.2.2 allows you to compare dates using differential operators
// Otherwise use DateTime::diff, http://php.net/manual/en/datetime.diff.php
if($start == $end){
    echo "Same!\n";
}
else {
    echo "Not Same!\n"
}
?>
Kyle
  • 3,935
  • 2
  • 30
  • 44
  • by this method can i commare dates from array , i have already booked dates in one array and new booking dates in other array , i want to compare that. – shurti saini Jul 30 '16 at 12:29
-1

this is my fanction to do that The function return a clause to date diffrent

you can compare some date and the current in 2 ways

timeDiff('123556568') // time()

timeDiff('20-6-2020') // any date format

timeDiff('20-6-2020','20-2-2120') // two dates 

timeDiff('143556568','123556568') // two dates 



    function timeDiff($time2_, $time1_ = false) {
            $date1 = new \DateTime();
            if ($time1_) {
// check if it timestamp or not 
                $time1 = preg_match('/[-\/]/', $time1_) ? strtotime($time1_) : $time1_;
                $date1->setTimestamp($time1);
            }
            $date2 = new \DateTime();
            $time2 = preg_match('/[-\/]/', $time2_) ? strtotime($time2_) : $time2_;
            $date2->setTimestamp($time2);
            $interval = $date1->diff($date2);
// get diffrence
            $diffrent_ = $interval->format('%Y Years %m Months %d Days %H:%I:%S');
// clean from zero values

            $diffrent = str_replace(['00 Years', ' 0 Months', ' 0 Days', '00:00:00'], '', $diffrent_);
// some change for 01 & s
            $returner = str_replace(['01 Years', ' 1 Months', ' 1 Days'], ['01 Year', ' 1 Month', ' 1 Day'], $diffrent);
            return trim($returner);
        }
  • Would be helpful if you provided some type of explanation or detail about your code and why it works. Just providing straight code might answer the question but it would be a lot more helpful if you provided more detail about your code and why it works. – Charlie Fish Aug 09 '16 at 03:20
  • you ar right i had updated the post now – Homam Alhaytham Aug 09 '16 at 09:21