Welcome to WebmasterWorld Guest from 54.196.224.166

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

If time is between X and Y BUT check that it's a certain time zone

     

416bc

11:31 pm on Sep 14, 2010 (gmt 0)



I'm trying to use php to display an image depending on time of day. It's for a live chat image. Here is the code I'm tweaking. I didn't write it.
function date_at_timezone($format, $locale, $timestamp=null){

if(is_null($timestamp)) $timestamp = time();

//Prepare to calculate the time zone offset
$current = time();

//Switch to new time zone locale
$tz = date_default_timezone_get();
date_default_timezone_set($locale);

//Calculate the offset
$offset = time() - $current;

//Get the date in the new locale
$output = date($format, $timestamp - $offset);

//Restore the previous time zone
date_default_timezone_set($tz);

return $output;

}

//Examples
$t = time();

print date("g:i A T", $t); //4:16 PM PDT
print date_at_timezone("g:i A T", "America/Los_Angeles", $t); //7:16 PM EDT
print date_at_timezone("g:i A T", "Pacific/Samoa", $t); //12:16 PM SST
print date("g:i A T", $t); //4:16 PM PDT

$time = date(H);
if ($time <18 $time > 22)
{
display_one_thing();
}
else
{
display_other_thing();
}


Can someone help me? I'm thinking something along these lines
$time = date(H:i);
if ($time < 08:30 || $time > 17:00)
{
display_one_thing();
}
else
{
display_other_thing();
}

But how do I make that check for only the 'America/Los_Angeles' time zone?

416bc

11:32 pm on Sep 14, 2010 (gmt 0)



Title should read BUT not BUY, sorry.

416bc

4:52 am on Sep 15, 2010 (gmt 0)



Found my own answer:
<?php
function date_at_timezone($format, $locale, $timestamp=null){

if(is_null($timestamp)) $timestamp = time();

//Prepare to calculate the time zone offset
$current = time();

//Switch to new time zone locale
$tz = date_default_timezone_get();
date_default_timezone_set($locale);

//Calculate the offset
$offset = time() - $current;

//Get the date in the new locale
$output = date($format, $timestamp - $offset);

//Restore the previous time zone
date_default_timezone_set($tz);

return $output;

}

$t = time();
$time = date_at_timezone("Hi", "America/Los_Angeles", $t);
if ($time > '0830' && $time < '1700')
{
echo 'Business Hours';
}
else
{
echo 'After Hours';
}

?>

Matthew1980

7:38 am on Sep 15, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there 416bc,

Welcome to WebmasterWorld!

Cool that you have figured this out; I shall only offer a little tweek here, It's not going to affect the function, but it's good practice to do something like this:-

if (($time > 0830) && ($time < 1700)){
echo 'Business Hours';
exit;
}
else{
echo 'After Hours';
exit;
}


Use parenthesis in the if clause especially if you are using logical operators [php.net ], this makes the evaluation easier to read & the exits serve a good purpose too; this stops the script executing any other code further down and just makes it tidier IMO. This is particularly advantageous if you have a header(); always good to exit after a header!

Cheers,
MRb

416bc

5:55 pm on Sep 15, 2010 (gmt 0)



Thanks. I had those there at one point, but in all my testing I deleted them. Thanks for the information.