Solved

Incorrect datetime value: storing date from php in MySQL

Posted on 2006-11-09
6
2,429 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now