Solved

Incorrect datetime value: storing date from php in MySQL

Posted on 2006-11-09
6
2,445 Views
Last Modified: 2013-12-12
Hi,
I am trying to store date in the MySQL. Here is my code to get start time and end time(5 min later)
$start = time();
$day=date("d",$start);
$year=date("y",$start);
$month=date("m",$start);
$hour=date("H",$start);
$min=date("i",$start);
$sec=date("s",$start);
$start = date("y-m-d H:i:s",$start);

$end=mktime($hour,$min+5,$sec,$month,$day,$year);
$end=date("y-m-day H:i:s",$end);

I have tried to add $start and $end to MySQL table with attribute type 'datetime' or 'timestamp'. I have tryed just a timestamp format(time()) or other formats for $start,$end values, but it always gives the error:
Incorrect datetime value: '06-11-09am06 07:44:11' for column 'end_time' at row 1

How to store date in MySQL?
We are running: PhpMyAdmin 2.8.2.4, MySQL 5.0, Php 5.1.6, Apache 2.0.5.8

Thank You,
0
Comment
Question by:margotsk
6 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 350 total points
ID: 17906039
$end=date("y-m-d H:i:s",$end);

-r-
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 50 total points
ID: 17906473
Use a capital Y in that format string given by Roonaan.

As a side note, the strtotime function looks handy here:
$start = date("Y-m-d H:i:s");
$end = date("Y-m-d H:i:s", strtotime("+ 5 minutes"));
0
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 50 total points
ID: 17906874
Alternatively, I tend to do the conversion to/from UNIX timestamps in the database, so I'd keep $end as just a timestamp and build the query like:

$query = "INSERT INTO the_table VALUES (FROM_UNIXTIME($end))";
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 50 total points
ID: 17908796
SELECT NOW();

That will give you a datetime. Why not use the database to handle dates and times instead of the app? You might also want to search the MySQL manual for Date and Time Functions to see what is available to you.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17908957
Hey, that's a good point... I didn't think through what that code actually does.  There's no reason PHP has to get involved at all.

NOW() would indeed be the start time.
To get the ending time, just use: DATE_ADD(NOW(), INTERVAL 5 MINUTE)
0
 

Author Comment

by:margotsk
ID: 17925476
Thank You,
Roonaan catch my typo and aswered to this quesion, but from all other replays I benefited as well. So, I would like split points as following:
Roonaan -350pts
VoteyDisciple -50pts
i_m_aamir -50pts
snoyes_jw -50pts

 Thank You again
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question