Solved

Date problem in PHP and in MySQL table 2011

Posted on 2011-09-10
11
406 Views
Last Modified: 2013-12-13
Below is the code that gives the date in PHP but o's in my table in MySQL.

// Get the current date.
            // $date1 = date('Y m d'); // year as 4 digits, month as 2 digits and day as 2 digits
      $date1 = date('m/d/Y'); // year as 4 digits, month as 2 digits and day as 2 digits

I have worked about one hour so far on this problem.

0
Comment
Question by:KennethSumerford1
  • 7
  • 4
11 Comments
 

Author Comment

by:KennethSumerford1
Comment Utility
Here is what it looks like in my date field in the MySQL DB table:  0000-00-00

This is the result in the Insert statement in the SQL in PHP:  '09/10/2011'  

In the structure of the prospect table it looks like this (field and format):  
OrigContactDate       date
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
When inserting a date into date or datetime field in MySQL, you need to format the date as YYYY-mm-dd

$date1=date('Y-m-d');
mysql_query( "INSERT INTO TableName(lastUpdate) VALUES('{$date1}')") or die( mysql_error() );
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
I did not see your previous update earlier:
mysql_query( "INSERT INTO tableName(OrigContactDate) VALUES('{$date1}')") or die( mysql_error() );

PS: When "extracting" the date, if you want it as mm/dd/yyyy, you can use:
SELECT DATE_FORMAT(`OrigContactDate`, '%m/%d/%Y') as `OrigContactDate` FROM tableName
0
 

Author Comment

by:KennethSumerford1
Comment Utility
The server is at Go Daddy running Windows, not Lennux. Could there be something else in the structure of the table to make the date not register correctly in the date field?
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
try something like this (but provide the correct tableName):
$sql="INSERT INTO tableName(OrigContactDate) VALUES('{$date1}')";
echo 'attempting to execute '.htmlentities($sql,ENT_QUOTES);
mysql_query($sql) or die( mysql_error() );

What do you get?
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

 

Author Comment

by:KennethSumerford1
Comment Utility
I still get the value of  0000-00-00  in the date field of the table when  I tried
$date1=date('Y-m-d');

$SQL1 .= ", '" . $date1 . "' "; // OrigContactDate

Do I need  #  in this SQL statement?
0
 

Author Comment

by:KennethSumerford1
Comment Utility
Maybe it is taking the date as a string.  
0
 
LVL 82

Accepted Solution

by:
hielo earned 200 total points
Comment Utility
>>Do I need  #  in this SQL statement?
Not for MySQL. Are trying to insert the current time? If so, try inserting Now().

Instead of:
$SQL1 .= ", '" . $date1 . "' "; // OrigContactDate

use:
$SQL1 .= ", Now() "; // OrigContactDate
0
 

Author Comment

by:KennethSumerford1
Comment Utility
$SQL1 .= ", '{$date1}' "; // OrigContactDate

This still gives me     0000-00-00
0
 

Author Comment

by:KennethSumerford1
Comment Utility
2011-09-10

$SQL1 .= ", '{$date1}' "; // OrigContactDate    and
$SQL1 .= ", Now() "; // OrigContactDate  
both work!  
0
 

Author Closing Comment

by:KennethSumerford1
Comment Utility
Good solution!  The date in PHP to MySQL can be tricky.

Thanks for your help.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
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…

728 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