The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP/MySQL - storing date and time
Discuss PHP/MySQL - storing date and time in the PHP Development forum on Dev Shed. PHP/MySQL - storing date and time PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 10th, 2000, 06:39 AM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: Hong Kong
Posts: 73
Time spent in forums: 6 h 31 m 9 sec
Reputation Power: 14
|
|
|
Umm...I have no idea on how to store the date/time in MySQL database.
Anyone got a sample code which deal with time/date in PHP/MySQL?
Indeed, I am making a tiny guest book like program. I want to store/retreive time and date in/from MySQL database.
|

March 10th, 2000, 07:39 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: Bochum, NRW, Germany
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
You need a special field in your table from the variable type DATETIME.
You can fill this with an extra MYSQL function named NOW() like this:
mysql_query("insert into table values (..., NOW(''),...");
The function NOW() insert the actual date/time, but you can even save every date/time by entering manually.
The data is stored in the format: YYYY-DD-MM HH:MM:SS
Check any MySQL tutorial for info.
Hope that will help you...
Martin
|

March 10th, 2000, 12:04 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: Hong Kong
Posts: 73
Time spent in forums: 6 h 31 m 9 sec
Reputation Power: 14
|
|
|
Thanks Martin.
now() works fine for me.
I just wonder if I can convert the format
of the date/time
i stored my date/time in the format of
YYYY-MM-DD HH:MM:SS
I want to show it in this format:
MM-DD-YYYY HH:MM in PHP
anyone know how to do it?
|

March 10th, 2000, 12:41 PM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
select date_format(date_column, '%m %d %Y %l:%i %p) as When from table_name;
// %p gives AM/PM if you need it
Check out MySQL's date and time functions on the online manual for more formatting options (military time, ordinal suffixes, a bunch of stuff)
|

March 10th, 2000, 08:54 PM
|
|
Junior Member
|
|
Join Date: Mar 2000
Location: Hong Kong
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks Kyuzo. (This is 'ccm'. I am using another account in office.)
Indeed, I am a newbie in both PHP and MySQL.
I have already created a table with a column named 'date datetime'.
And I have used NOW() in PHP to store the date/time info in MySQL database.
For now, I retreive the date/time info by '$row->date' in PHP.
I have no idea what to do next.
Do I have to re-create the whole table?
And how do I implement the Date/Time format in my table?
|

March 10th, 2000, 11:59 PM
|
 |
.Net Developer
|
|
Join Date: Feb 2000
Location: London
Posts: 987
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
|
|
|
Hi hkccm,
You don't have to recreate the table for changing a field type.
u may try this..
'alter table tblname change old_field new_field TIMESTAMP(-);'
for getting a special date format you can use
TIMESTAMP--with in bracket pass value for a specific format.
See the mysql documentation for the correct syntax.
|

March 13th, 2000, 03:32 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: Bochum, NRW, Germany
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
To change the date/time format you can even use the regular expression stuff, it's quite mighty!
Example:
$date = "2000-03-13";
ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$date, $regs);
echo "$regs[3].$regs[2].$regs[1]";
Output: 13.03.2000
This function takes the date ($date) and splits it into an array ($regs) by odering after YYYY ($regs[1]), MM($regs[2]), DD($regs[3]) - there was a failure in my first posting ;-)).
You can now easily change the order by changing the order of the array.
Martin
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|