Results 1 to 14 of 14

Thread: PHP Date

  1. #1
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    PHP Date

    Guys... I've got dates going into a table (stock_date, mysql db) in this format...

    2008-05-02 10:01:01

    How can I format the output on a PHP page to d/m/y H:i

    I've tried <?php echo date('d/m/y H:i');($row_rs_stock['stock_date']); }?>

    ...but that just echo's today's date...

    any ideas?

  2. #2
    Spinal Pap Tomahawk's Avatar
    Join Date
    Jul 2003
    Location
    Bristol/Manchester Uni
    Posts
    1,002
    Thanks
    8
    Thanked
    13 times in 8 posts

    Re: PHP Date

    You can do it in SQL or PHP.. I usually do it in the former; here goes using your example:
    MySQL Example
    Code:
    DATE_FORMAT(yourtable.date, '&#37;d/%m/%Y %H:%i') AS date
    This is simply formatting the date from the field 'yourtable.date' and assigning it the alias 'date'. (The above goes in the SELECT line of the query).

    PHP Example
    Code:
    date('d/m/Y H:i', $stock_date);
    This is just using the 'date' function within PHP, you pass the pattern you want initially then second parameter is the variable/date you want formatted; but I've got a feeling the $stock_date must be a timestamp.. so maybe best to format at the SQL end.

    PHP: date - Manual

    Hope that helps.


    [ iTomaHawk | My Music MySpace ]

  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP Date

    I hate a date with a girl who was a PHP'er, didn't go well.....


    But in serious nature, you shouldn't really do this at the SQL level, instead do it with the PHP date command, as Tomahawk says, you simply specify the date you want formatted as the last argument.

    (its bad to format things at the database level like that because they should be abstract there)
    throw new ArgumentException (String, String, Exception)

  4. #4
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,276
    Thanks
    292
    Thanked
    837 times in 473 posts

    Re: PHP Date

    If you want to get dates out of the database in unix timestamp format so that you can bung them through PHP date() easily, use
    Code:
    UNIX_TIMESTAMP(field) AS name
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  5. #5
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP Date

    or use PHP strtotime
    Code:
    printf("&#37;s", date('d/m/Y H:i', strtotime($stock_date)));
    To err is human. To really foul things up ... you need a computer.

  6. #6
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    Re: PHP Date

    Quote Originally Posted by Tomahawk View Post

    PHP Example
    Code:
    date('d/m/Y H:i', $stock_date);
    Hope that helps.


    Many thanks, but when using this i get:

    01/01/1970 01:00



  7. #7
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,276
    Thanks
    292
    Thanked
    837 times in 473 posts

    Re: PHP Date

    Get the field out using UNIX_TIMESTAMP, or parse it with strtotime, then that will work.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  8. #8
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: PHP Date

    Quote Originally Posted by Vini View Post
    Many thanks, but when using this i get:

    01/01/1970 01:00


    Because it's been passed an illegal value so it treats it as zero. Convert your MySQL 'dates' to a unix timestamp and it'll work fine, actually it's much better storing all your temporal data as timestamps, personally, I'd update the database and remove the date field.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  9. #9
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    Re: PHP Date

    I don't have control over the date going into the SQL db as it's going from our ERP > Excel > mySQL > Output PHP

  10. #10
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP Date

    Not sure whether you got this so...

    Quote Originally Posted by yamangman View Post
    or use PHP strtotime
    Code:
    printf("%s", date('d/m/Y H:i', strtotime($stock_date)));
    To err is human. To really foul things up ... you need a computer.

  11. #11
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    Re: PHP Date

    Quote Originally Posted by yamangman View Post
    Not sure whether you got this so...
    Cheers, but that's giving:

    05/05/2008 00:00

    Should be 05/05/2008 16:01 (this ERP > Excel > mySQL > PHP runs every hour)

  12. #12
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP Date

    Well that's the fault of the date-time formatted string you are providing to the function.... try RTFM.
    To err is human. To really foul things up ... you need a computer.

  13. #13
    Custom User Title
    Join Date
    Oct 2005
    Location
    Wirral UK
    Posts
    1,168
    Thanks
    10
    Thanked
    14 times in 14 posts
    • cougarslam's system
      • Motherboard:
      • Asus Maximus Formula SE (ROG)
      • CPU:
      • Core 2 Duo E6600 @ 3ghz
      • Memory:
      • 4gb Corsair DDR2
      • Storage:
      • 1TB
      • Graphics card(s):
      • BFG Nvidia 8800GT OC 512MB
      • PSU:
      • Corsair HX520
      • Case:
      • Zorro
      • Operating System:
      • Vista Business 32
      • Monitor(s):
      • 2 x 17" crt
      • Internet:
      • adsl max (entanet)

  14. #14
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: PHP Date

    Quote Originally Posted by Vini View Post
    I don't have control over the date going into the SQL db as it's going from our ERP > Excel > mySQL > Output PHP
    Sorry, been at work all day. Well that sucks.. Looks like you'll need to use the MySQL UNIX_TIMESTAMP() function to get the job done, although I'd see about annoying whoever wrote the crazy Excel->MySQL import system to use this function on INSERT, rather than you having to query it later, it'll speed up MySQL->Php queries, and make coding time-related problems much, much, easier.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. The Well Dodgy Joke Thread
    By 0iD in forum General Discussion
    Replies: 1246
    Last Post: 08-04-2009, 01:42 PM
  2. MySQL extension for PHP 5.2.3 not working
    By Jerrythafast in forum Help! Quick Relief From Tech Headaches
    Replies: 18
    Last Post: 13-06-2007, 08:03 PM
  3. PHP and file uploads timing out too soon
    By McClane in forum Software
    Replies: 12
    Last Post: 02-12-2006, 05:57 PM
  4. Image Upload With Date (PHP)
    By realm in forum Software
    Replies: 6
    Last Post: 11-09-2006, 04:42 PM
  5. Mouse with a life of it's own.
    By firefox in forum PC Hardware and Components
    Replies: 10
    Last Post: 23-03-2006, 12:49 PM

Posting Permissions

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