Solved

Incorrect datetime value: storing date from php in MySQL

Posted on 2006-11-09
6
2,456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Problem 10 76
How do I Import CSV File In my PHP Application 29 74
How can I stuff the results of this for loop into an array? 3 42
Conditional Array Element 7 28
This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

739 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