Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Determining if a timestamp exists in a range of timestamps, and if so, where exactly?

    I'm in the process of writing a script. The $ts variables below are timestamps that correspond to beginning of each hour of the day. For example, $ts6AM is the time stamp for today at exactly 6AM, and so on.

    The variable $now contains the time stamp for right now. The very next step in writing this script is to tell php to determine if $now exists between $ts6AM and $ts6PM. If it does exist, where exactly is it?

    For example, is it right after $ts8AM? Is it exactly at $ts5PM? Is it between $ts12PM and $ts1PM?

    Does anyone know how to do this?
    Thank you

    Here is the code I've written so far:
    PHP Code:
    <?php
    if(isset($_POST["check"]))
    {
        
        
    $M3 date("M"); //Jan, Feb, etc
        
    $today date("j"); //Finds todays date (From 1-31)
        
    $Y date("Y"); //Finds todays year(20xx)
        
    $hour date("g"); //12 hour format of an hour (1-12)
        
    $AM date("A"); //UPPERCASE AM or PM

        
    $numeric_month date("n");//Numeric Month (1-12)

        
    $ts6AM mktime(6,0,0$numeric_month$today$Y);

        
    $ts7AM mktime(7,0,0$numeric_month$today$Y);

        
    $ts8AM mktime(8,0,0$numeric_month$today$Y);

        
    $ts9AM mktime(9,0,0$numeric_month$today$Y);

        
    $ts10AM mktime(10,0,0$numeric_month$today$Y);

        
    $ts11AM mktime(11,0,0$numeric_month$today$Y);

        
    $ts12PM mktime(12,0,0$numeric_month$today$Y);

        
    $ts1PM mktime(13,0,0$numeric_month$today$Y);

        
    $ts2PM mktime(14,0,0$numeric_month$today$Y);

        
    $ts3PM mktime(15,0,0$numeric_month$today$Y);

        
    $ts4PM mktime(16,0,0$numeric_month$today$Y);

        
    $ts5PM mktime(17,0,0$numeric_month$today$Y);

        
    $ts6PM mktime(18,0,0$numeric_month$today$Y);
     
        
    $now time();




       
    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I would use the DateTime handling myself, although the api itself is quite unstable on the minor versions of PHP so you may not have the functions available.
    In either case, these can be compared as integers. Therefore you can compare them using simple comparison operators:
    PHP Code:
    if ($now >= $ts6AM && $now <= $ts10AM)
    {
        print 
    'Now is between $ts6AM and $ts10AM';

    Simple as that.

    With DatePeriod and DateInterval, you can check (or populate) with an iterative approach:
    PHP Code:
    date_default_timezone_set('UTC');

    $dtStart = new DateTime('06:00');
    $dtEnd = new DateTime('18:00');
    $dtNow = new DateTime();
    $di = new DateInterval('PT1H');
    $dp = new DatePeriod($dtStart$di$dtEnd);

    $dtPrev $dtStart;
    foreach (
    $dp AS $date)
    {
        
    printf("%s lands between %s and %s? %d" PHP_EOL
            
    $dtNow->format(DateTime::W3C),
            
    $dtPrev->format(DateTime::W3C),
            
    $date->format(DateTime::W3C),
            (
    $dtNow >= $dtPrev && $dtNow <= $date)
        );
        
    $dtPrev $date;

    You could also extend the DateTime and write a between(DateTime, DateTime) method.

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks very much for your help. I used a switch case statement. This php script is one part of an ajax script. The php script below does what I need it to do. It finds the current timestamp and returns the hour of the day that timestamp falls within. I'm studying the code in your reply. Thanks for taking the time to write it

    I just learned there is a javascript switch/case. I'll use that on the js side.

    Here is the script that works:
    PHP Code:
    <?php
    if(isset($_POST["check"]))
    {
        
        
    $M3 date("M"); //Jan, Feb, etc
        
    $today date("j"); //Finds todays date (From 1-31)
        
    $Y date("Y"); //Finds todays year(20xx)
        
    $hour date("g"); //12 hour format of an hour (1-12)
        
    $AM date("A"); //UPPERCASE AM or PM

        
    $numeric_month date("n");//Numeric Month (1-12)

        
    $ts6AM mktime(6,0,0$numeric_month$today$Y);

        
    $ts7AM mktime(7,0,0$numeric_month$today$Y);

        
    $ts8AM mktime(8,0,0$numeric_month$today$Y);

        
    $ts9AM mktime(9,0,0$numeric_month$today$Y);

        
    $ts10AM mktime(10,0,0$numeric_month$today$Y);

        
    $ts11AM mktime(11,0,0$numeric_month$today$Y);

        
    $ts12PM mktime(12,0,0$numeric_month$today$Y);

        
    $ts1PM mktime(13,0,0$numeric_month$today$Y);

        
    $ts2PM mktime(14,0,0$numeric_month$today$Y);

        
    $ts3PM mktime(15,0,0$numeric_month$today$Y);

        
    $ts4PM mktime(16,0,0$numeric_month$today$Y);

        
    $ts5PM mktime(17,0,0$numeric_month$today$Y);

        
    $ts6PM mktime(18,0,0$numeric_month$today$Y);
     
        
    $now time();



        if(
    $now >= $ts6AM && $now <= $ts6PM)
        {
             switch(
    $now)
             {
                 case(
    $now >= $ts6AM && $now $ts7AM):
                      echo 
    "6AM - 6:59AM";
                      break;

                 case(
    $now >= $ts7AM && $now $ts8AM):
                      echo 
    "7AM - 7:59AM";
                      break;

                 case(
    $now >= $ts8AM && $now $ts9AM):
                      echo 
    "8AM - 8:59AM";
                      break;

                 case(
    $now >= $ts9AM  && $now $ts10AM):
                      echo 
    "9AM - 9:59AM";
                      break;

                 case(
    $now >= $ts10AM && $now $ts11AM):
                      echo 
    "10AM - 10:59AM";
                      break;

                 case(
    $now >= $ts11AM && $now $ts12PM):
                      echo 
    "11AM - 11:59AM";
                      break;

                 case(
    $now >= $ts12PM && $now $ts1PM):
                      echo 
    "12PM - 12:59PM";
                      break;

                 case(
    $now >= $ts1PM  &&  $now $ts2PM):
                      echo 
    "1PM - 1:59PM";
                      break;

                 case(
    $now >= $ts2PM  &&  $now $ts3PM):
                      echo 
    "2PM - 2:59PM";
                      break;

                 case(
    $now >= $ts3PM  &&  $now $ts4PM):
                      echo 
    "3PM - 3:59PM";
                      break;

                 case(
    $now >= $ts4PM  &&  $now $ts5PM):
                      echo 
    "4PM - 4:59PM";
                      break;

                 case(
    $now >= $ts5PM  &&  $now $ts6PM):
                      echo 
    "5PM - 5:59PM";
                      break;
             }
        }
        else
        {
          echo 
    "no match";
        }


        
    //$server = $M3.$today.$Y.$hour.$AM.'_list';
        //echo $server;
        //echo $ts6PM;
    }
    ?>
    Last edited by milesdriven; 10-01-2012 at 12:46 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •