Solved

Date problem in PHP and in MySQL table 2011

Posted on 2011-09-10
11
410 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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 200 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your 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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

828 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