tags:

views:

330

answers:

5

My initial approach was:

$current = time(); // save this to column CURRENT_TIME with column type VARCHAR

//retrieve it like this
$retrieved = mysql_query(....) //assume query for getting the stored time value
$time = strtotime($retrieved);

I have come across the following approaches:

  1. use gmstrftime to handle gmt
  2. use INT instead of VARCHAR for the column
  3. use the mysql function CURTIME or CURDATE
  4. use the UNIX_TIMESTAMP mysql function

none of which were using the DATETIME or TIMESTAMP mysql var type.

Do you have a better approach for this one?

A: 

Well, you can turn a MySQL TIMESTAMP field into a PHP Time() value by using strtotime()

Then you just have to make a function that correctly turns a PHP Time() value into a MySQL TIMESTAMP value.

Chacha102
and the function to turn a PHP time() value into a mysql TIMESTAMP would be?
Ygam
@Ygam: you can use MySQL's `FROM_UNIXTIME()` function to convert for storage into the DB. You can also actually use `UNIXTIMESTAMP()` to convert coming out, as well, if you want to do it DB-side.
Amber
@Ygam You could have Googled this.... :`$mysqldate = date( 'Y-m-d H:i:s', $phpdate );`
Chacha102
I have already done that, but the solution I was looking for was to have a timestamp as datatype in mysql and then be able to format it with the date() function:(
Ygam
A: 

Storing it as int is more logical. You save the row format of date, using which you can later extract other format and more data .. I also save it as int.

eGaMaL
then you never build sites for multiple timezones
zerkms
timestamp and date datatypes were invented for several reasons.. plus it give other people an idea of what the data is about.
Cesar
+3  A: 

it is recommended to use mysql timestamp (YYYY-MM-DD HH:MM:SS) field type to store time and date variables in mysql.

$sDate = date("Y-m-d H:i:s");
mysql_query("insert into `table_name` set `created_on` = '$sDate'");

it gives you ability to use mysql functions to compare dates, calculate time differences and so more directly in your mysql query.

and this field type makes no problem in php code (you can always use strtotime() to retriev it's timestamp value)

takpar
so to store it in the database I need to use date('YYYY-MM-DD HH:ii:ss) so that it can be compatible with the mysql timestamp? (because I have tried other formats and the column remains 0000-00-00 00:00:00)
Ygam
i eddited the answer. take a look.also check manual at http://dev.mysql.com/doc/refman/5.0/en/datetime.html
takpar
got it working, and exactly what I was looking for. Thanks!
Ygam
A: 

The format used by MySQL doesn't need to match the format used by PHP. Choose the best on either side. Databases have date data types for a reason; in MySQL you can choose from these:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html

In PHP, you basically have three options:

  • Good old timestamps are easy to handle and you can use them in your MySQL queries —see FROM_UNIXTIME() and UNIX_TIMESTAMP()— but they have serious range issues (you can't rely on them for pre-1970 dates, so they are unsuitable for birthdays).

  • DateTime objects are powerful and builtin, have no range issues and support time zones. However, they are sometimes not very comfortable to use since they seem to lack some important methods.

  • You can use a custom date object (third-party or your own DateTime based).

Álvaro G. Vicario
+1  A: 

I just use the TIMESTAMP value type in MySQL, and let MySQL use its own CURRENT_TIMESTAMP.

henasraf