Solved

Incorrect datetime value: storing date from php in MySQL

Posted on 2006-11-09
6
2,464 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

627 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