WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP code is using UTC not Local (9 posts)

  1. paulson101
    Member
    Posted 1 year ago #

    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.

  2. pete_398
    Member
    Posted 1 year ago #

    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'));

  3. paulson101
    Member
    Posted 1 year ago #

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

  4. pete_398
    Member
    Posted 1 year ago #

    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 ?

  5. paulson101
    Member
    Posted 1 year ago #

    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.

  6. pete_398
    Member
    Posted 1 year ago #

    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>
  7. paulson101
    Member
    Posted 1 year ago #

    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.

  8. pete_398
    Member
    Posted 1 year ago #

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

  9. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

    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.)

Topic Closed

This topic has been closed to new replies.

About this Topic