?
Solved

Date problem in PHP and in MySQL table 2011

Posted on 2011-09-10
11
Medium Priority
?
416 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
[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
  • 7
  • 4
11 Comments
 

Author Comment

by:KennethSumerford1
ID: 36517906
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
ID: 36517911
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
ID: 36517916
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:KennethSumerford1
ID: 36517951
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
ID: 36517976
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
 

Author Comment

by:KennethSumerford1
ID: 36517979
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
ID: 36517981
Maybe it is taking the date as a string.  
0
 
LVL 82

Accepted Solution

by:
hielo earned 800 total points
ID: 36517992
>>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
ID: 36517996
$SQL1 .= ", '{$date1}' "; // OrigContactDate

This still gives me     0000-00-00
0
 

Author Comment

by:KennethSumerford1
ID: 36518006
2011-09-10

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

Author Closing Comment

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

Thanks for your help.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this article, we’ll look at how to deploy ProxySQL.
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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