WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] PHP code is using UTC not Local

[Resolved] PHP code is using UTC not Local

  • paulson101
    Member

    @paulson101

    I am using some code I found to try to have a special of the day:

    ‘<?php $today = date(‘N’);
    if( $today == 1) { ?>
    <div class=”monday”>monday</div>
    <?php } elseif( $today == 2 ) { ?>
    <div class=”tueday”>tuesday</div>
    <?php } elseif( $today == 3 ) { ?>
    <div class=”wednesday”>wednesday</div>
    <?php } elseif( $today == 4 ) { ?>
    <div class=”thursday”>thursday</div>
    <?php } elseif( $today == 5 ) { ?>
    <div class=”friday”>friday</div>
    <?php } elseif( $today == 6 ) { ?>
    <div class=”saturday”>saturday</div>
    <?php } elseif( $today == 7 ) { ?>
    <div class=”sunday”>sunday</div>
    <?php } ?>’

    Problem is at 4PM Pacific time, it turns to midnight UTC and it changes to the next day.

    I have some code:

    ‘<?php echo date(‘H:i’, current_time(‘timestamp’));?>’

    I am using to display the time right above it and it is working fine.

    I REALLY need some help here, have tried almost everything and I need to get this working to launch the site.

Viewing 8 replies - 1 through 8 (of 8 total)
  • If this works ..

    <?php echo date('H:i', current_time('timestamp'));?>

    then change the top line from

    <?php $today = date('N');

    to

    <?php $today = date('H:i', current_time('timestamp'));

    paulson101
    Member

    @paulson101

    ok changed that, but now it isn’t outputting anything. Any other ideas? Even for achieving the same effect?

    I ran that code, with the top line modified. It is looking for a function called current_time() . Do you have the code for that ?

    Also, what timezone do you want it in ?

    paulson101
    Member

    @paulson101

    to get it to be PST I had Peter from Silver Maple Design Help:

    ‘<?php $today = date(‘N’, strtotime(‘- 7 hours’));
    if( $today == 1) { ?>
    <div class=”monday”>monday</div>
    <?php } elseif( $today == 2 ) { ?>
    <div class=”tueday”>tuesday</div>
    <?php } elseif( $today == 3 ) { ?>
    <div class=”wednesday”>wednesday</div>
    <?php } elseif( $today == 4 ) { ?>
    <div class=”thursday”>thursday</div>
    <?php } elseif( $today == 5 ) { ?>
    <div class=”friday”>friday</div>
    <?php } elseif( $today == 6 ) { ?>
    <div class=”saturday”>saturday</div>
    <?php } elseif( $today == 7 ) { ?>
    <div class=”sunday”>sunday</div>
    <?php } ?>’

    This works perfect, hope it helps others in the future.

    Messy code though; a bit more elegant way to do it ..

    <?php
    $days = array(
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
    );
    
    date_default_timezone_set('UTC');
    $today = date('N', strtotime('- 7 hours'));
    $todaysname = $days[$today];
    ?>
    <div class="<?php echo $todaysname ?>"><?php echo $todaysname ?></div>
    paulson101
    Member

    @paulson101

    The reason I think for the original way, is because I have complex divs inside the areas where the day is, with different rotating information for each. The label was more of a place holder.

    For that, simple have a multi-dimensional array, with the complex div information, it will keep the code clean.

    WPyogi
    Forum Moderator

    @wpyogi

    Hey Paulson – please use the code buttons when posting code here – it can cause problems here and corrupt your code:

    http://codex.wordpress.org/Forum_Welcome#Posting_Code

    (and mods won’t have to chase around fixing it for you 🙂 — thanks.)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] PHP code is using UTC not Local’ is closed to new replies.