SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    durango
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Datetime and am/pm issue

    Hi all,

    I am saving the date and time that a user logs in into a MySql database table. The field is set to "datetime" and I am setting the value like this:

    $date = date ("Y-m-d h:i:s");

    Here is the SQL Insert:

    PHP Code:
     $query_Insert2 "INSERT INTO login (login_session_ID, login_users_ID, login_sec_level, login_teach_ID, login_scol_ID, login_date, login_time, login_remote_host, login_remote_IP )
        VALUES ('
    $session','$users_ID','$sec_level','$teach_ID','$scol_ID','$date','1','$host','$ip')"
    The problem that I am having is that the time showing is always shown as AM, except the 12:00 hour that shows as PM. Thus, it looks like users are logging in at 1:30 in the morning when they are really logging in at 1:30 in the afternoon.

    Here is the SQL for displaying the results:

    PHP Code:
    $query_recSessions sprintf("SELECT *, DATE_FORMAT(login_date, '%%c/%%e/%%y %%r') as date_time2 FROM login WHERE login_users_ID = '%s' ORDER BY login_date DESC"$colname_recSessions); 
    When I look in the database, the datetime field simply shows "2008-05-08 01:55:09" There is no AM/PM or 24 hr clock so I don't know what is morning or night.

    So, is the problem with my script or the database? And, how do I fix it to save the right time of day?

    Also, since I have the user's general location in their profile can I either save to or display from the database their correct time based on their time zone. Right now everyone sees the server time which is US/Pacific.

    Thanks,

    sk
    If I only had a brain.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    date ("Y-m-d h:i:s"); 
    The lowercase 'h' means to represent it as a 12-hour clock.

    Replace the 'h' with either 'G' or 'H' to fix this - the 'G' being double-digit (e.g. 01.53) and the 'H' being single (1.53).
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    durango
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that was easy!

    Do you know about the time zone issue?

    Also, since I have the user's general location in their profile can I either save to or display from the database their correct time based on their time zone. Right now everyone sees the server time which is US/Pacific.
    Thanks,

    sk
    If I only had a brain.

  4. #4
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at this function:
    date_default_timezone_set()


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
  •