-3

I have two dates i want to compare between two of them are they at the same time (year/month/day/hour) i get time from database and the other time from user input i have used

$checkdate = strtotime($meetingDate);
$dateconflicts = strtotime($_SESSION['date']);

if ($checkdate == $dateconflicts ) {
    $msg = "<div class='alert alert-danger'>there's conflicts !</div>";                
}

the thing is it doesn't work like this i don't know why.

WEBjuju
  • 5,797
  • 4
  • 27
  • 36
ziyad
  • 29
  • 6
  • `strtotime()` returns a timestamp, which means that you are comparing two timestamps. So unless the datetime you've given is the exact same second, it will fail. You probably want to format it with dates instead, I recommend the [DateTime](http://php.net/manual/en/datetime.construct.php) class. – Qirel Dec 04 '16 at 19:22
  • it is answer this question : http://stackoverflow.com/questions/8722806/how-to-compare-two-dates-in-php – hasan movahed Dec 04 '16 at 19:29
  • value of `$_SESSION['date']` is what here? and was the session started? Edit: value of `$meetingDate` is what also? – Funk Forty Niner Dec 04 '16 at 19:41
  • $_SESSION['date'] from previous page the user enter date to create a new meeting in this time $meetingDate is date from database every user has a time slot that they are busy in this time – ziyad Dec 04 '16 at 19:44

2 Answers2

0

If you are looking for how to check if 2 dates have the same hour :

$date1 = '2016-12-04 20:21:10';
$date2 = '2016-12-04 21:50:00';

if (date('Y-m-d H', strtotime($date1)) == date('Y-m-d H', strtotime($date2))) {
    echo 'Same hour';
}
Max
  • 891
  • 8
  • 17
  • 1
    I think the OP wants the same day, year, month as well. `they at the same time (year/month/day/hour)` – chris85 Dec 04 '16 at 19:25
  • You are right @chris85, I updated my answer :) – Max Dec 04 '16 at 19:28
  • 1
    OP is using sessions here `$_SESSION['date']` so we don't know if that array contains a value or not and as to what it is, is anyone's guess. – Funk Forty Niner Dec 04 '16 at 19:41
  • Yes @Fred-ii-, this is true. Hopefully this will work, if `$_SESSION['date']` contains any kind of standard formatted date – Max Dec 04 '16 at 19:44
  • 1
    I've asked for clarification in comments up there @Max and as I am typing this out, OP responded but isn't clear on that neither. All they wrote was: *"$_SESSION['date'] from previous page the user enter date"*. – Funk Forty Niner Dec 04 '16 at 19:45
  • 1
    thank you guys i have solved my problem. both of your solution fixed my problem. – ziyad Dec 04 '16 at 19:49
0

To ignore the seconds, create a string from the integer of time that only includes the Year, Month, Date, and Hour:

$checkdate = date('Y-m-d H', strtotime($meetingDate));
$dateconflicts = date('Y-m-d H', strtotime($_SESSION['date']));

if ( $checkdate == $dateconflicts ) {
    $msg = "<div class='alert alert-danger'>The dates $checkdate and $dateconflicts conflict !</div>";   
}
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • i have done your solution and now the condition is always true no matter what. – ziyad Dec 04 '16 at 19:38
  • please provide example data. – WEBjuju Dec 04 '16 at 19:41
  • @ziyad i updated the `$msg` so that you can eyeball what you are getting out of your dates. if you are getting something unexpected, then the problem may be in the dates well before this code runs. – WEBjuju Dec 04 '16 at 19:43
  • thank you now i have done it with ur solution with a little bit of modify in my database – ziyad Dec 04 '16 at 19:48