Solved

Incorrect datetime value: storing date from php in MySQL

Posted on 2006-11-09
6
2,443 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

773 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