Web Analytics Made Easy -
StatCounter Date compare - how do you do this in php? - CodingForum

Announcement

Collapse
No announcement yet.

Date compare - how do you do this in php?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date compare - how do you do this in php?

    Greetings again experts,

    I hope this is not a huge challenge for php gurus.

    I was given two dates in the following formats:
    Code:
    startdate = '09/19/2016 07:00:00 AM'
    $enddate = '10/14/2016 11:59:59 PM'
    As long as the current date and time are inside the startdate and enddate, voting can go on.

    However, voting cannot start before the startdate and cannot continue after the end date.

    I was able to get this to work in asp.net but struggling to figure out how to do in php.

    Any assistance is greatly appreciated. Below is the code:

    Code:
    $now = time();
    startdate = '09/19/2016 07:00:00 AM'
    $enddate = '10/14/2016 11:59:59 PM'
    
    if ($now < strtotime($startdate) || $now > $enddate) {
        header('location:done.php');
    }

  • #2
    Use strtotime ...
    PHP: strtotime - Manual

    and don't use AM PM. Use 24hr mode. 23:59:59 instead of 11:59:59 PM

    I believe the recommended date format for the U.S. is: mm-dd-yyyy
    Your date format, example something like 08/09/2016 ... is that Aug 9th, or Sept 9th?

    View the php.net manual and see what they say about recommended formats, whether U.S. or U.K. style

    Comment


    • #3
      I looked up and found this format:

      Code:
      echo date("m/d/Y h:i:s A");
      When I ran it, I got:

      09/16/2016 03:54:47 PM

      So, something like this should work

      Code:
      $now = date("m/d/Y h:i:s A");
      $startdate = '09/19/2016 07:00:00 AM';
      $enddate = '10/14/2016 11:59:59 PM';
      
      if ($now < strtotime($startdate) || $now > $enddate) {
          header('location:done.php');
      }
      except that when I run it, it kept redirecting me to done.php; it doesn't matter what date I tested with.

      This worked and still works flawlessly in my .net app.

      Only difference between the two, I think is the syntax, no?

      Code:
              Dim times As New Date
              times = Now
      
              If times < "5/13/2015 8:00:00 AM" Or times > "5/15/2015 5:00:00 PM" Then
                  Response.Redirect("done.aspx")
              End If

      Comment


      • #4
        You need to convert $endate with strtotime as well


        PHP Code:
        $now time();
        $startdate '09/19/2016 07:00:00 AM';
        $enddate '10/14/2016 11:59:59 PM';

        if (
        $now strtotime($startdate) || $now strtotime($enddate)) {
            
        header('location:done.php');

        Last edited by jimhill; Sep 16, 2016, 06:06 PM.
        If you can't stand behind your troops, feel free to stand in front of them
        Semper Fidelis

        Comment


        • #5
          @jimhill ... good catch. I didn't even notice that at first.

          Comment

          Working...
          X