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.
oo7mlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What is the connection between the server time and the value that you echo?

Up to 02:00 it seems you are using consecutive odd numbers for 30 min intervals but after this things go a bit wonky
$servertime = 02:01 to 02:30 echo 11    OK
$servertime = 02:31 to 03:00 echo 12    WHY 12 NOW DID YOU MEAN 13?
$servertime = 03:01 to 00:30 echo 15   DID YOU MEAN 03:01 to 03:30 OR TIME WARP?

Open in new window


How is your server  time stored?

Assuming that you want odd value increments and the above were typos then the algorithm for working this out is
<?php
$servertime = '2015-07-29 00:59:14';

$ServerTimeInSeconds = strtotime($servertime) ;
$ServerTimeInMinutes = $ServerTimeInSeconds / 60;

$MidnightInMinutes = strtotime(Date('Y-m-d', $ServerTimeInSeconds) . '00:00:00') / 60;
$minutes = ($ServerTimeInMinutes - $MidnightInMinutes);
$value = ((int)(($minutes-1)/30) + 1) * 2 + 1;

echo "Value is {$value}<br/>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oo7mlAuthor Commented:
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.
0
Julian HansenCommented:
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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Ray PaseurCommented:
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
oo7mlAuthor Commented:
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

0
Julian HansenCommented:
$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.
0
Julian HansenCommented:
Actually we could simplify it to
(int)(($minutes - 1) / 15) + 3

Open in new window

0
oo7mlAuthor Commented:
Ok, thanks Julian.

So my number always increase, and never be less than a previous number?
0
Julian HansenCommented:
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.
0
Ray PaseurCommented:
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

0
Ray PaseurCommented:
Here is how I generated the array of times and numbers.
<?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>';

// CREATE AN ARRAY OF TIMES
$times = range(0, (60*60*24)-1, 30*60);
foreach ($times as $key => $secs)
{
    $times[$key] = date('H:i', strtotime("TODAY + $secs SECONDS"));
}

// CREATE AN ARRAY OF DATA VALUES
$datas = range(1, count($times)*2, 2);

// COMBINE TO SET TIMES AS KEYS
$final = array_combine($times, $datas);
$final = array_reverse($final);
var_export($final);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.