Jump to content

Simple timedate problem


bugzy

Recommended Posts

Ok I got this column "date_reg" on my table which has a "datetime" type.

 

It's giving me a value like "2012-04-20 04:28:03"

 

The problem is, when I'm trying to insert on that column using this format

 

<?php $datenow = date('m/d/Y h:i:s a', time()); ?>

 

it's giving me a value of "0000-00-00 00:00:00" which is suppose to be the current date and time the user add on that table.

 

 

Anyone knows how I can fix this?

Link to comment
Share on other sites

04/22/2012 12:35:43 am

Is what it said when I ran the code on my server using your code.

 

I think this is what it really wants:

<?php $datenow = date('Y-m-d H:i:s', time()); ?>

Which on my server resulted in:

2012-04-22 00:38:35

Link to comment
Share on other sites

The DATETIME column is limited to YYYY-MM-DD HH:MM:SS format.  Anything else is illegal.

 

Simply insert it that way, then format it as you desire when showing it to the user.

 

You can also store it as a unix timestamp and format it as well.  Many people find a unix timestamp to be easier to work with when doing advanced mathematics.

Link to comment
Share on other sites

04/22/2012 12:35:43 am

Is what it said when I ran the code on my server using your code.

 

I think this is what it really wants:

<?php $datenow = date('Y-m-d H:i:s', time()); ?>

Which on my server resulted in:

2012-04-22 00:38:35

 

 

Thanks! :)

Link to comment
Share on other sites

The DATETIME column is limited to YYYY-MM-DD HH:MM:SS format.  Anything else is illegal.

 

Simply insert it that way, then format it as you desire when showing it to the user.

 

You can also store it as a unix timestamp and format it as well.  Many people find a unix timestamp to be easier to work with when doing advanced mathematics.

Yes, unix timestamp is nice, but both is stored the same way and handled almost the same way. Though I think too a unix timestamp is easier to work with, but it's not as easy to look at with the human eye in the database. You can make the DATETIME return unix time by using UNIX_TIMESTAMP().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.