SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Date time method

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date time method

    Hi,

    I am trying to insert date and time into a table, see below:

    PHP Code:
    $date date(time());
    $hours date('h',$date);
    $mins date('m',$date);
    $secs date('s',$date);

    $_POST['date'] = $_POST['year'].'-'.
    $_POST['month'].'-'.$_POST['day'].' '.$hours.':'.$mins.':'.$secs
    The date is selected by the user via a calendar, it all works fine. But when i check the inserted row it inserts like this:

    2010-09-20 03:09:55
    The date is fine but the time is wrong, it should have added it like this:

    2010-09-20 15:48:55
    I think the time formate may be wrong? I need it to be in UK time format.

    Any ideas how i can do this?

    Thanks
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Crikey.
    PHP Code:
    <?php
    $date 
    sprintf(
      
    '%04d-%02d-%02d %s',
      
    $_POST['year'],
      
    $_POST['month'],
      
    $_POST['day'],
      
    date('H:m:s')
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is so much simpler than what i had! :-)

    Thanks
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As always Billy, you're most welcome.

    For reference, you should have been using an upper-case H. Additionally, I'd also say the time element of the date string is probably not needed. Considering you're making it up.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

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
  •