note 10582 added to Date and Time functions

From: Date: Thu, 11 Jan 2001 16:00:09 +0000
Subject: note 10582 added to Date and Time functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-9654@lists.php.net to get a copy of this message
I had some problems with dates between mySQL and PHP. PHP had all these great date functions but I wanted to store a usable value in my database tables. In this case I was using TIMESTAMP(14) <or 'YYYYMMDDHHMMSS'>. This is perhaps the easiest way I have found to pull the PHP usable UNIX Datestamp from my mySQL datestamp stored in the tables: Use the mySQL UNIX_TIMESTAMP() function in your SQL definition string. i.e. $sql= "SELECT field1, field2, UNIX_TIMESTAMP(field3) as your_date FROM your_table WHERE field1 = '$value'"; The query will return a temp table with coulms "field1" "Field2" "your_date" The "your_date" will be formatted in a UNIX TIMESTAMP! Now you can use the PHP date() function to spew out nice date formats. Sample using above $sql: 20010111002747 = Date Stored on mySQL table (TIMESTAMP(14)) 979172867 = value returned as your_date in sql stmt (UNIX_TIMESTAMP) if we use $newdate = date("F jS, Y -- g:ia", $row["your_date"]); --(after fetching our array from the sql results of course)-- echo "$newdate"; --Will produce: January 11th, 2001 -- 12:27am Hope this helps someone out there! http://www.php.net/manual/ref.datetime.php

« previous php.notes (#9654) next »