A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearGetting a PHP DateTime object into a string representation of a MYSQL date - page 1
User InfoPosts
Getting a PHP DateTime object into a string representation of a MYSQL date#1
I have a DateTime which I want to store in a Date MySQL column. I am using MySQLi and prepared statements.

When binding parameters, I cannot specify date as a type, only strings and integers.

How do I convert the DateTime to a MySQL date string representation? There is very little documentation on the DateTime class.

posted date: 2008-12-28 12:01:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-28 12:01:01


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#3
I almost closed this as a duplicate of http://www.momige.com/136782/format-mysql-datetime-with-php but I think it's actually the reverse problem. That question asked how to format a date fetched from MySQL, and you're asking how to format a date for input to a query.Option 1:SELECT * FROM MyTable WHERE DateTimeCol = ?You can format the value to the YYYY-MM-DD HH:MM format MySQL wants:<?php $mysql_date = date('Y-m-d H:i:s', $timestamp); ?>Then submit it as a string parameter to the query.Option 2:SELECT * FROM MyTable WHERE DateTimeCol = FROM_UNIXTIME(?)Then you can submit the timestamp value as a numeric parameter to the query.Option 3:SELECT * FROM MyTable WHERE DateTimeCol = STR_TO_DATE(?, '%m/%d/%y')You can submit a wide variety of string representations of date/time, if you specify the formatting to MySQL's STR_TO_DATE() function. For the formatting codes, see the DATE_FORMAT() function.

posted date: 2008-12-28 12:20:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#4
Almost closed this as a duplicate question, then I realized it was different and I reopened it. Please excuse my haste!

posted date: 2008-12-28 12:24:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#5
Because the DateTime-class introduced with PHP5 is poorly documented, i wouldn't recommend to use it. I find regular timestamps to be much easier to work with!But if you still want to use DateTime-objects, a solution would be to transform the objects into (unix) timestamps before storing them in your database. Transform the timestamps back to DateTime-objects after reading information from your database.To create DateTime-object from a timestamp, use date_create() with the timestamp as argument. To get your objects representation of number of seconds since the Unix Epoch, use date_format($object, 'U') (uppercase U).

posted date: 2008-12-28 12:49:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#6
shouldn't it be 'Y-m-d H:i:s'? (php-date.com/#mysql)

posted date: 2008-12-28 13:32:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#7
Second are optional, but yes that works. If you use 'H' (12-hour clock) instead of 'G' (24-hours clock), then you should also include AM/PM. Refer to DATE_FORMAT() documentation for full list of codes.

posted date: 2008-12-29 10:33:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#8
'h' is 12-hour clock, 'H' is 24 with leading zeros and 'G' is 24 without leading zeros. You should indeed use 'H'.

posted date: 2009-02-02 05:46:00


Re: Getting a PHP DateTime object into a string representation of a MYSQL date#9
I've edited the format string in option 1 above with respect to these comments from ʞɔıu and christian studer.

posted date: 2009-02-02 09:07:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.