1

On my website, after a dateselection, the date is given to the server by an AJAX post call. The format before handing it over looks like this: 08/09/2018 08/24/2018

According to the XHR in my debugging console, the payload when returning these dates from the serverside looks like this: ["08/09/2018","08/24/2018"]

Now, I must admit that I dont know what exactly these dates look like while they "live" inside the php script called by my AJAX. But I guess they look at least somehow like the payload.

however, I inside this called php script, the date coming from the javascript is compared to one I get from the database. The date coming from the database looks a bit different, like this: 2018-06-04 00:00:00

Now, does a comparison (with ">" or "<" for example) work while using these two different formats? Or do I need to unify the format before the comparison? If so, how can I do that? Currently, the comparison implemented inside this php script doesnt work, but I dont know whether this is due to another bug or due the formal differences between the dates.

The MYSQL Code doing the comparison inside my php script looks like this.

 $options = $connection->query("
  select arb.nummer
  from arbeitsplatz arb
  LEFT JOIN (
   SELECT res.arbeitsplatz
   FROM reservierung res
   WHERE (res.anfang >= '".$start."' AND res.anfang <= '".$ende."')
   OR (res.ende >= '".$start."' AND res.ende <= '".$ende."')
   OR (res.anfang <= '".$start."' AND res.ende >= '".$ende."')
   AND (res.status = 'angefragt' OR res.status='belegt')
 ) AS respre
 ON arb.id = respre.arbeitsplatz
 WHERE respre.arbeitsplatz IS NULL
 AND arb.aktiv = 1
 AND arb.raum = '".$raum."'
 AND (arb.art = '".$art."'
 OR art = 'beides')
 ORDER BY arb.nummer ASC");

edit: sry the first post lacked some of the MYSQL code.

ForeFather321
  • 179
  • 2
  • 9
  • You need them in the same format. This question covers a lot of the things you're asking. https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime – Howard P Aug 02 '18 at 12:01
  • For date to date comparision, you need to convert the dates to `yyyy-mm-dd` format for correct results. – Salman A Aug 02 '18 at 12:01
  • _“Now, does a comparison (with ">" or "<" for example) work while using these two different formats?”_ - you mean, compare an apple (`08/09/2018`) to an orange (`2018-06-04`) …? No, of course that won’t work. And even if the format `08/09/2018` was used consistently, it would not work, because that is not a “sortable” date format. These aren’t numeric values, so only text comparison rules can be used for such > < comparisons, and that works character-by-character, from left to right. – CBroe Aug 02 '18 at 12:01
  • 1
    okay thanks :=) Sry Im really new to this php/MySQL stuff and Im thankful I didnt need to test that out ^ Ill rewrite the code so that the dates are unified :=) – ForeFather321 Aug 02 '18 at 12:03

0 Answers0