WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Local Time (10 posts)

  1. Nasir Zia
    Member
    Posted 2 years ago #

    Hi,
    What is the shortest code to display the current local time on my homepage?

    Thanks

  2. Robert Bossaert
    Member
    Posted 2 years ago #

    Depends in which format you want it to display, see Formatting Date and Time

  3. Nasir Zia
    Member
    Posted 2 years ago #

    I want it like this 12:10PM

  4. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    <script language="javascript">
    ourDate = new Date();
    document.write("The time and date at your computer's location is: "
    + ourDate.toLocaleString()
    + ".<br/>");
    document.write("The time zone offset between local time and GMT is "
    + ourDate.getTimezoneOffset()
    + " minutes.<br/>");
    document.write("The time and date (GMT) is: "
    + ourDate.toGMTString()
    + ".<br/>");
    </script>

    Update as needed...example of this code is now running at http://swansonheritage.com/ in upper widget

  5. Nasir Zia
    Member
    Posted 2 years ago #

    Sorry, i should have mentioned this on the beginning of this thread..
    I am using Cubepoints plugin to assign points to user... Now i want users to get points once they update their profile info... But i want users filling in profile info before 30th june 23:59 to receive 100 points, and users filling in their profile info after that time should receive just 10 points... So i used this code.

    if ( time( ) > strtotime( "05/30/2012 11:59PM" ) ) {
    // You are late, just few points for you...
    cp_alterPoints(cp_currentUser(), 10);
    }
    else {
      // You are before time... You will get bonus points..
      cp_alterPoints(cp_currentUser(), 100);
    }

    The piece of code above works fine for me.. Although the time is using PHP time function which is usually the server time. My local time is UC+5, so how could i make it happen the right way?

    NOTE: I have an option to hard code the time in a way that it works fine with my local time like 1st July 05:00 AM, but it would be better if its the localtime...

    Thanks

  6. Digital Raindrops
    Member
    Posted 2 years ago #

    @SwansonPhoto
    Nice code but I am not sure where the GMT is coming from, I am in the UK so my local time is GMT, but your code shows GMT - 1 Hour.

    The time and date at your computer's location is: 11 June 2012 08:26:24.
    The time zone offset between local time and GMT is -60 minutes.
    The time and date (GMT) is: Mon, 11 Jun 2012 07:26:24 UTC.

    The two GMT labels should be 'UTC', this would be correct for my location (UK - GMT)

    The time and date at your computer's location is: 11 June 2012 08:26:24.
    The time zone offset between local time and UTC is +60 minutes.
    The time and date (UTC) is: Mon, 11 Jun 2012 07:26:24

    For the GMT date I use date_i18n() also look at formatting options.

    <?php /* Our Current Date and Time */ ?>
    <span class="current-date"><?php echo date_i18n('j F Y', time()); ?></span>

    HTH

    David

  7. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    I think most folks who run contests simply are specific that the deadline is your local time (there are actually too many oddities in what can happen with time)...I would just inform folks of that and use local server time. Anything more is a hassle. In your disclaimer of such rule, link them to a table/website with the time offsets...

  8. Digital Raindrops
    Member
    Posted 2 years ago #

    @Nazir,
    It might be easy to add a Countdown Widget with Time Zone Days Hours and Minutes instead of a date, then the selected time zone would be the key, if you are in the UK but 60% of traffic was from the USA you could set a Time Zone?

    Or just take the code from the plugin!

    HTH

    David

  9. Nasir Zia
    Member
    Posted 2 years ago #

    Looks a bit hassle (and more coding).. I think i should just stick with the plan B, which is to hard code the time in a way that it works fine with my local time..

    Thanks David & SwansonPhotos.

    Good to see Swanson quite active on WP forums these days ;)

  10. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    I had an extended schedule for a few months which worked out well...'tis summer here...

Topic Closed

This topic has been closed to new replies.

About this Topic