SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date time difference

    Hi, this is probalbly simple, but when I enter:
    PHP Code:
    <?
    // format timestamp with date()
    echo date("h:i a M d, Y"mktime());
    ?>
    instead of saying it's 5:50pm, it says it's 6:50pm (this is probalby because my server is in a diffent time zone. How do I format this to show the correct time in my zone?

  2. #2
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webgodjj
    Hi, this is probalbly simple, but when I enter:
    PHP Code:
    <?
    // format timestamp with date()
    echo date("h:i a M d, Y"mktime());
    ?>
    instead of saying it's 5:50pm, it says it's 6:50pm (this is probalby because my server is in a diffent time zone. How do I format this to show the correct time in my zone?
    I found this function:

    If you get the current date in a different timezone, try this function:
    PHP Code:
    <?php
    /*
    PHP Code:
    [font=Courier New]$current should be the timezone on the
    current machine
    $target is the timezone
    you want to calculate
    .  Both should be in
    hours
    .  For exampleGMT -05:00 should
    be 
    -5.  GMT +12:00 should be 12.  You can
    use the returned timestamp with the date
    function.

    ex: echo date('r'zonechange(-510));
    would echo the date for +10:00
    timezone on a machine in 
    -05:00.
    */

    function 
    zonechange ($current$target) {
    $current = -$current;
    $zonedate mktime(date('G'), date('i'), date('s'), date('n'),
    date('j'), date('Y'), 1) + (($current $target) * 3600);
    return 
    $zonedate;
    }[/
    font]
    [
    font=Courier New]?>[/font] 


    here:

    http://www.php.net/manual/en/function.date.php

    Close?

    (I have no idea where those font tags are coming from. Grrr... )

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't want a function doing it, try:
    PHP Code:
    echo date("h:i a M d, Y"mktime(date("h")-1)); 
    -Helge

  4. #4
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like the function... however implementing it seems difficult.

    I have a database with a field called timestamp that is a datetime format. I insert the value with the now() function.

    How would I use this funciton to convert that time? Or would I need something different?

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's because unfortunately MySQL uses the server's local time for its functions. this is why i've started using an INT column and storing a Unix timestamp that PHP's time() returns.

    but where doing the INSERT, you could subtract an hour like this (instead of just NOW()):

    DATE_SUB(NOW() INTERVAL 1 HOUR)

    i hope that's the correct syntax.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't I already answer this question over at DevShed?

  7. #7
    SitePoint Member Rocanion's Avatar
    Join Date
    Sep 2003
    Location
    New Hampshire, USA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The lovely function provided by Herbster has a couple deadly oversimplifications that will cause problems if you're trying to write code for a truly global audience;

    1. It does not account for changes to/from daylight savings time. In locales that change clocks the function will return an incorrect time for part of the year.

    2. It does not account for timezones whose offset from GMT (more correctly UTC) is something other than an integral number of hours. Timzone offsets are properly calculated using a combination of hours and minutes since some timezones are offset from their neighbors by half an hour and a few by 15 minutes.

    I'm still looking for a really good method of handling time that accounts for item 1 in particular.

  8. #8
    SitePoint Member
    Join Date
    Jan 2004
    Location
    USA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solution Found?

    Did anyone find a solution to this?

  9. #9
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    SitePoint Member
    Join Date
    Jan 2004
    Location
    USA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know about gmmktime. The problem is that I don't know how to adjust the time based on desired time zone. For instance, the server is current in CDT, but I need to be able to adjust it for cases when the date needs to be in Eastern or Mountain or Pacific time. I using gmmktime and date as such:

    $timestamp = gmmktime($hour_expires, $minute_expires, 0, $current_month, $day_expires, $current_year);
    $time_alert_expires = date("F d, Y, h:i A", $timestamp);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •