Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display timezone in php

I want to display 7 different time for 7 different countries, but as you can see on my code I have just a simple knowledge of programming. My question is, is there a better way of displaying the time for this 7 countries(Sydney,Brisbane,Perth,Bangkok,London,New York, and L.A.) and can I use more than one different date_default_timezone_set in one php file? because on what I tried it only follows one timezone. Appreciate all the help, thanks.

<html>
    <head></head>
    <title>PA Toolbox</title>
<?php
function a(){
if(date_default_timezone_set('Australia/Sydney'))
    {
    date("format");
    echo date( 'h:ia');
    }
}

function b(){
    if(date_default_timezone_get('Australia/Brisbane'))
    {
     $date = date(“format”, $timestamp); 
     echo date( 'h:ia');
   }    
}
//else
//elseif(date_default_timezone_get('Australia/Perth'))    
//    {
//     $date = date(“format”, $timestamp);
//    }
//elseif(date_default_timezone_get('Asia/Bangkok'))    
//    {
//     $date = date(“format”, $timestamp);
//    }
//elseif(date_default_timezone_get('Europe/London'))    
//    {
//     $date = date(“format”, $timestamp);
//    }
//elseif(date_default_timezone_get('America/New_York'))    
//    {
//     $date = date(“format”, $timestamp);
//    }
//elseif(date_default_timezone_get('America/Los_Angeles'))    
//    {
//     $date = date(“format”, $timestamp);
//    }    
?>

    <table border="1">
    <tr>    
    <td>Sydney and Melbourne</td><td>Brisbane</td><td>Manila and Perth</td> <td>Bangkok</td><td>London</td><td>New York</td><td>Los Angeles and San Francisco</td>  
    </tr>
    </>
    <td><?php return a(); ?></td><td><?php return b(); ?></td>
    <td><?php echo date( 'h:ia'); ?></td><td><?php echo date( 'h:ia'); ?></td>
    <td><?php echo date( 'h:ia'); ?></td><td><?php echo date( 'h:ia'); ?></td>
    <td><?php echo date( 'h:ia'); ?></td>
    </tr>    
    </table>
</html>

The clock works fine, but how do I design it so that the numbers move without refreshing the page?


1 Answers

If you use the DateTime class, you don't need to change your default timezone every time:

date_default_timezone_set('YourOwn/LocalTimezone'); // fill in the blank

$timezones = array(
    'Sydney'    => 'Australia/Sydney',
    'Melbourne' => 'Australia/Melbourne',
    ...
);

foreach ($timezones as $name => $timezone) {
    $localTime = new DateTime('now', new DateTimeZone($timezone));
    echo "$name: ", $localTime->format('h:ia');
}
like image 134
deceze Avatar answered Feb 19 '26 12:02

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!