Link to home
Start Free TrialLog in
Avatar of oo7ml
oo7ml

asked on

Different Number Based On Server Time

Hi,

I am trying to echo a number on my site which is calculated / based by the current server time.

I have very basic PHP coding skills... i can read and interpret most PHP code but not the best when it comes to writing it :-)

I am looking to echo the following:

IF

$servertime = 00:01 to 00:30 echo 3
$servertime = 00:31 to 01:00 echo 5
$servertime = 01:01 to 01:30 echo 7
$servertime = 00:31 to 02:00 echo 9
$servertime = 02:01 to 02:30 echo 11
$servertime = 02:31 to 03:00 echo 12
$servertime = 03:01 to 00:30 echo 15

etc...
 
Can anyone else with a fully executed script for this? Thanks in advance for your help.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oo7ml
oo7ml

ASKER

Hi, thanks for your reply.

Yes i want to manually input odd numbers for each 30 min period, but each number needs to be greater that the number for the previous 30 min period.

As long as the number increased by 2, 4 or 7 per 30 mins i would be happy.
As long as the number increased by 2, 4 or 7 per 30 mins i would be happy.
So the posted code does do what you want or it doesn't?

Here is an online sample of the code posted. Enter a date value into the box provided as

yyyy-mm-dd hh:mm:ss (2015-07-29 00:29:15)

Click submit to see the returned value. Is this in line with what you are wanting to do?

Sample here
PHP Date/Time values are accurate the the second, and from the question it's not completely clear whether the times you're describing are hours:minutes or minutes:seconds.

This article can help with the basic understanding of date/time in PHP.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Avatar of oo7ml

ASKER

Thanks, i have it working now with Julian's code.

Julian, can you elaborate or breakdown the following sentence please

$value = ((int)(($minutes-1)/30) + 1) * 2 + 1;

Open in new window

$value = ((int)(($minutes-1)/30) + 1) * 2 + 1;

This is just a formula we use to create the value (sequential odd numbers based on 30 min intervals).

Lets break it down
(int) - this means take the integer part of the expression that follows so everything to the left of the decimal point. We will elaborate on this further.

The next bit ($minutes - 1) / 30

We are incrementing in 30 minute intervals so we divide our minutes by 30 to get an index as to which interval we happen to be in but this can result in an ambiguous answer as demonstrated below.

Lets say the server time is 00:27:00 i.e. we are in the 27th minute. To find out which interval we are in we divide by 30 like so

27/30 = 0.9

If we take the integer portion of the answer we get 0 (everything to the left of the decimal)

Now consider being in the 30th minute (00:30:00) - it should be the interval as above but 30/30 = 1 - so we have two potential answers 0 and 1 for the same interval.

Therefore we subtract 1 from the minutes so 27 becomes 26/30 and 30 becomes 29/30.

Now we will always get a value < 1 for interval 1 - 30 and a value < 2 for 31 to 60 and so forth.

To get rid of the decimal we use the (int) hence
(int)(($minutes - 1) / 30)

This will give the following
01 - 30 => 0
31 - 60 => 1
61 - 90 => 2

Open in new window


Based on your later comments - this might be good enough - however if you specifically need to translate those numbers into sequential odd numbers starting from 3 you need to
add 1
x 2
add 1

Open in new window

(0 + 1) x 2 + 1 = 3
(1 + 1) x 2 + 1 = 5
(2 + 1) x 2 + 1 = 7

Open in new window

This gives us the rest of the formula.
Actually we could simplify it to
(int)(($minutes - 1) / 15) + 3

Open in new window

Avatar of oo7ml

ASKER

Ok, thanks Julian.

So my number always increase, and never be less than a previous number?
With both those methods - yes it will relative to the day you are in so as soon as server time clocks over to a new day the interval will start from 1.

If this is not the correct behaviour post back and I will explain how to make it so the interval is unique across days as well.
This seems to make sense to me.  You may want to consider the consequences of the transitions in daylight savings time.
<?php // demo/temp_oo7ml.php

/**
 * http://www.experts-exchange.com/questions/28701115/Different-Number-Based-On-Server-Time.html
 */
error_reporting(E_ALL);
echo '<pre>';

function time_to_value($datetime='NOW')
{
    static
    $table
    = array (
    '23:30' => 95,
    '23:00' => 93,
    '22:30' => 91,
    '22:00' => 89,
    '21:30' => 87,
    '21:00' => 85,
    '20:30' => 83,
    '20:00' => 81,
    '19:30' => 79,
    '19:00' => 77,
    '18:30' => 75,
    '18:00' => 73,
    '17:30' => 71,
    '17:00' => 69,
    '16:30' => 67,
    '16:00' => 65,
    '15:30' => 63,
    '15:00' => 61,
    '14:30' => 59,
    '14:00' => 57,
    '13:30' => 55,
    '13:00' => 53,
    '12:30' => 51,
    '12:00' => 49,
    '11:30' => 47,
    '11:00' => 45,
    '10:30' => 43,
    '10:00' => 41,
    '09:30' => 39,
    '09:00' => 37,
    '08:30' => 35,
    '08:00' => 33,
    '07:30' => 31,
    '07:00' => 29,
    '06:30' => 27,
    '06:00' => 25,
    '05:30' => 23,
    '05:00' => 21,
    '04:30' => 19,
    '04:00' => 17,
    '03:30' => 15,
    '03:00' => 13,
    '02:30' => 11,
    '02:00' => 9,
    '01:30' => 7,
    '01:00' => 5,
    '00:30' => 3,
    '00:00' => 1,
    )
    ;
    $time = strtotime($datetime);
    if (!$time)
    {
        trigger_error("INVALID DATETIME VALUE: $datetime", E_USER_WARNING);
        return FALSE;
    }
    $hhmm = date('H:i', $time);
    foreach ($table as $key => $value)
    {
        if ($hhmm < $key) continue;
        return $value;
    }
}

// TEST THE FUNCTION
var_dump(time_to_value());
var_dump(time_to_value('Today 10:29'));
var_dump(time_to_value('Today 10:30'));
var_dump(time_to_value('Today 10:31'));
var_dump(time_to_value('Today 11:00'));

var_dump(time_to_value('YESTERDAY 00:00:00'));
var_dump(time_to_value('Wednesday 23:59:59'));
var_dump(time_to_value('crump'));

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial