0

I am trying to insert a PHP date into MySQL date value column, but for this I need to format it correctly and make sure I have the right time, for instance, I need to use mysql_real_scape_string(date("h:i:s", time())); for the time and mysql_real_escape_string(date("Y-m-d")); for the date itself.

This has caused me little to no problem before now, but all of a sudden, it is not inserting rows because the format is wrong, and also the time has jumped back an hour, say it is 12:40:00, say I echo date("h:i:s", time()); it will tell me that the time is 11:40:00. What is the issue there?

  • 1
    Looks like it's a timezone issue, after the switch to daylight savings – Mark Baker Apr 20 '15 at 12:09
  • Also, you can simplify your date call like this - date('Y-m-d H:i:s'); – gamesmad Apr 20 '15 at 12:10
  • How would I go about doing this? – Samuel G Swift Apr 20 '15 at 12:11
  • I need the date to be split into 2 fields for the insert into the DataBase – Samuel G Swift Apr 20 '15 at 12:11
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 20 '15 at 12:13
  • @JayBlanchard, I cannot `"stop using mysql_* functions"` due to the fact that I have been given a large wordpress based site in which to work on, to change from mysql to PDO (or preferably MySQLi) would push back a release date for this by far too long. Also, your comment holds no relevance to the question, it is a blunt array of words that hold no meaning to what it is that I have asked for. I ask you to keep it relevant please, I want to be able to rectify this issue that I am having. – Samuel G Swift Apr 20 '15 at 12:16
  • 1
    When your server gets updated, as it inevitably will, the suggestion will be very relevant @SamuelGSwift ¯\\_(ツ)_/¯ I couldn't have possibly known you were using WordPress and your release date was not stated as a problem. – Jay Blanchard Apr 20 '15 at 12:19
  • @JayBlanchard, I am not saying that it won't be, the use of MySQL has been discussed with the client, and they are well aware that it will be removed, but for the time being they wish to get the site live ASAP, and therefore, MySQL is having to stay for now. MySQLi will be used after the launch date of the site due to the depreciation of MySQL functionality, but like I say, as it stands, the MySQL-MySQLi-PDO argument is not relevant to the issue at hand. – Samuel G Swift Apr 20 '15 at 12:22
  • Apologies - I was not arguing, just commenting. This area is for comments, right? – Jay Blanchard Apr 20 '15 at 12:23
  • @JayBlanchard, I did not mean to sound as if I said you were arguing, that was meant as a general developer argument, all I am asking is that we keep the discussion "on topic" and relevant to the question in hand – Samuel G Swift Apr 20 '15 at 12:24
  • Why do you need to escape dates? – Jay Blanchard Apr 20 '15 at 12:24
  • It is a safety measure implemented as a result of security concerns by the client, despite my best efforts to say that dates, especially generated inside PHP, are safe – Samuel G Swift Apr 20 '15 at 12:26
  • Are the dates inserted into a datetime column? – Jay Blanchard Apr 20 '15 at 12:27
  • The columns are of VARCHAR(15) data value – Samuel G Swift Apr 20 '15 at 12:28
  • The date is a date value column I should say, the time is of varchar(15) – Samuel G Swift Apr 20 '15 at 12:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75714/discussion-between-samuel-g-swift-and-jay-blanchard). – Samuel G Swift Apr 20 '15 at 12:29

1 Answers1

0

The problem is now fixed, all I had to do was add a small amount of validation to a parameter passed through to the method.

if ($param == null) { ... }
if ($param == "") { ... }