Solved

Date problem in PHP and in MySQL table 2011

Posted on 2011-09-10
11
407 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
category table 2 25
how can i gnupg encrypt a PDF document? 12 32
Create a MySQL table as easily as possible 6 37
What does this formatting equate to? 7 10
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 count occurrences of each item in an array.

773 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