MySql timestamp - different time after update all rows - php

I need update all rows in my events table. There is column event_date with timestamp datatype.
But if i update each row (event_date column) with new date (expample: 2015-12-12 12:00:00) then i have some rows with value 2015-12-12 13:00:00.
Point is - why are some rows correct and some are +1h?
In PHP i am using Nette framework and his DateTime object which extends standart PHP DateTime...
Any ideas, hints why this happends?
EDIT: query looks like this:
UPDATE `events`
SET `event_date`='2016-2-13 12:00:00', `event_date_to`=NULL
WHERE (`id` = 203)
Values in php i am setting like this:
$row->event_date = date("Y-m-d H:i:s", $oldRow['event_date']);
Problem starts sooner - in this table was dates like 2016-2-13 00:00:00 but after select and echo dates changed to 2016-2-12 23:00:00 - but no all rows... only someones. So i make select:
select events.id, events.event_date, events.event_date_to,
concat(year(event_date), '-', month(event_date), '-',
day(event_date), ' 12:00:00') as new_event_date,
IF(events.event_date_to IS NULL,null, concat(year(event_date_to),
'-', month(event_date_to), '-', day(event_date_to), ' 12:00:00')) as
new_event_date_to from events
That select give me rows like this:
769,2014-04-22 19:30:00,2014-04-22 21:45:00,2014-4-22 12:00:00,2014-4-22 12:00:00
It means: id, event_date(real db value), event_date_to(real db value), event_date(new value for insert), event_date_to(new value for insert - can be NULL)
And saved to csv file... This file i am parsing now and in foreach updating each row...
I checked ALL queries and times are OK (12:00:00) so i don't understand and stuck :)

Your MySQL timezone is probably dissimilar to that of your code (specified in php.ini).
To test this, try inserting this value into a DATETIME or TIMESTAMP column:
foo_column = NOW()
Concurrently, insert $datetime into a separate column - lets say $datetime simply equals the current time: date('Y-m-d H:i:s', time())
bar_column = '$datetime'
If the values are different - then there you have it. Your database is essentially interpreting time differently to your php.

Related

Why would SQL update the date into tabel to 0000-00-00 00:00:00?

I am trying to update the field SEEN in a SQL table to the current time and date.
Here is the code:
$now=date('d-m-Y H:i:s',time());
$query="UPDATE mytable SET SEEN = '".$now."' WHERE ID_ITEM = ".$id_material;
$stmt=$dbh1->prepare($query);
$query ;
$stmt->execute();
It sets my SEEN field to 0000-00-00 00:00:00.
If I write a specific date directly into the query, say "2021-03-10 02:30:00" it would write that date into the SEEN field. But instead of $now, it would output 0s. And $now is fine, it outputs the correct timestamp.
The format you are using is not the same that MySQL DATETIME uses:
$now = (new DateTime("now"))->format("Y-m-d H:i:s");

how to insert today date in mysql using php

How to insert today date in mysql using php.date format is like Dec-10-2013.i tried
$sql="insert into tbl_name (colm1,colm2) values (1,now())";
Here value inserted in colm2 is 2013-12-11.
What you want to do is save your dates as TIMESTAMP or something similar. When you retrieve it, you can format it. You can insert your dates with 'NOW()' or a given format if they are different.
If you want to display the time, you can do: new DateTime($row['my_date']);
For inserting you can use the same method: (new DateTime($date))->getTimestamp()
Why DateTime?
- Because it works with timezones.
Why not store it as Dec-10-2013?
- Because you cannot do anything with a varchar pretending to be a date. You have a TIMESTAMP field type for that
You can useDATE_FORMAT to format the date on the fly:
Change your query to:
INSERT INTO tbl_name (colm1,colm2)
VALUES (1, DATE_FORMAT(NOW(),'%b-%d-%Y'))
Example:
mysql> select DATE_FORMAT(NOW(),'%b-%d-%Y');
+-------------------------------+
| DATE_FORMAT(NOW(),'%b-%d-%Y') |
+-------------------------------+
| Dec-11-2013 |
+-------------------------------+
1 row in set (0.00 sec)
See the MySQL documentation for a list of available formatting options.
If you want to do it PHP-side, you can use date_create():
$date = date_create()->format('M-d-Y');
Use DATE_FORMAT(NOW(),'%b %d %Y') .
For more info Date_Format
I can see in your question that your db timezone format is YYYY-MM-DD
$date = date("Y-m-d", strtotime($yourDate));
$sql="insert into tbl_name (colm1,colm2) values (1,{$date})";
EDIT:
'Dec-10-2013' is known format for php, so you can do this $yourDate = 'Dec-10-2013';
This is as close as I can get to your desired output right now, but I'll keep trying. Maybe if you replace the date input with a different letter, just try a bunch.
$CurDate = date("r");
$sql="INSERT INTO tbl_name (colm1,colm2)
VALUES (1,'$CurDate')";
you can try this
$date = date("M-d-Y");
$sql="insert into tbl_name (colm1,colm2) values (1,$date)";
first create the timestamp of today date
$date= strtotime("now");
insert into tablename value({$date});
To print the date again from database use this code
echo date("Y-m-d",$date);

PHP Insert Query - Date field 0000-00-00

I'm having trouble inserting a record with a date field. No matter what I do, the date field gets stored as '0000-00-00'. The value comes into the php script as a string in this format:
'9/13/2013'. I'm converting to a date using strtotime, then trying to insert the record. Here's the code. Why can't I get this to work? Thanks!
$my_date = strtotime($_POST['d']);
//die(date("Y-m-d", $my_date)); //<--outputs just fine if I uncomment this
$sql = "INSERT INTO locations (id, my_date) VALUES ('$id', '$my_date')";
$result = mysql_query($sql);
I'm converting to a date using strtotime
strtotime returns a unix timestamp (integer), not a date string. You can format it for MySQL before inserting with the date function:
$my_date = date('Y-m-d H:i:s', strtotime($_POST['d']));
Alternatively, you can use the DateTime class for this purpose.

Store php datetime in mysql database

I can't believe I can't do this, but I want to be able to store the current date and time from php in to a mysql table.
The column in the table is type datetime.
I've tried this
$current_date = date("Y-m-d");
$my_date = strtotime($current_date);
INSERT INTO my_table (date_time) VALUES ('$my_date')
but my timestamp comes up as 0000-00-00 00:00:00
This must be so easy to do but I just can't get it working!
I want to use the timestamp from php rather than using the mysql now() function
Try this:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
In the date-format parameter of the date function, use :
'H' for 24hr format
'h' for 12hr format
Don't save it as the Unix Timestamp (which strtotime() outputs), but as "2012-12-02 13:00" into the DATETIME column.
Create column type TIMESTAMP and set it to NOT NULL. Then pass in NULL during INSERT and MySQL will insert current date and time. This works for me.
set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (`date_time`) VALUES (CURRENT_TIMESTAMP)
If you have the date in PHP as a timestamp, you can use the FROM_UNIXTIME function [1]
mysql> insert into table_name values (FROM_UNIXTIME(your_timestamp_here));
Hope it helped
[1]. https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
Remove the strtotime()
$current_date = date("Y-m-d");
INSERT INTO my_table (date_time) VALUES ('$current_date')
If you want to include the hour, minutes and seconds,
$current_date = date("Y-m-d H:i:s");

conversion of datetime datatype to date and time datatype in php/mysql

I am a newbie. I started php coding few days back. I want to copy a "datetime" datatype in php to fields that are of "date" and "time" datatype.
I have kept the field name datetime_info for the datetime value. It exists in try1 table. date is the name of field for "date" datatype and time is the name for "time" datatype. These two exist in try2 table.
Here is what I have written.
$result = mysql_query("SELECT * FROM try1");
while ($row = mysql_fetch_array($result))
{
$result_update = mysql_query("INSERT INTO try2 (date, time) VALUES ('".$row_team['datetime_info']."', '".$row_team['datetime_info']."')");
if (!$result_update)
die('Error: ' . mysql_errno() . mysql_error());
}
The values stored in "try1" table are:
id datetime_info
1 2008-10-02 00:00:00
2 2008-10-09 00:00:00
The expected response should be the date and time stored in respective fields. However, the output is
id date time
2 0000-00-00 00:00:00
3 0000-00-00 00:00:00
Can anyone explain me why and how? I tried a lot of sites but did not find any proper explanation for this. Thank you in anticipation.
Regards,
BasicGem
First of all, you shouldn't have to use PHP to do the hard work here. MySQL is very powerful to do these kinds of tasks on it's own.
Since you are copying all rows from table try1 into try2 (but splitting the datetime column), the following should work:
INSERT INTO
`try2`
SELECT
null, DATE( `datetime_info` ), TIME( `datetime_info` )
FROM
`try1`
What this does is: for every record found in try1 insert the following values into try2:
null means use the auto_increment functionality of the id column in try2 (this is presuming the id column is indeed an auto incrementing primary key field)
DATE( `datetime_info` ) means use the DATE part of the datetime_info column of try1
TIME( `datetime_info` ) means use the TIME part of the datetime_info column of try1
Simply doing mysql_query( $sql ) where $sql is a string that represents above query should suffice then. No need to loop through any results first with PHP.
I was gonna write a PHP solution as well, but others have already done so. I would have suggested this answer.
$result = mysql_query("SELECT * FROM try1");
while ($row = mysql_fetch_array($result))
{
$split = explode(' ', $row_team['datetime_info']);
$result_update = mysql_query("INSERT INTO try2 (date, time) VALUES ('". $split[0]."', '".$split[1]."')");
if (!$result_update)
die('Error: ' . mysql_errno() . mysql_error());
}
Because your taking it into a format and pushing it to fit another one.
Just use explode() and you'll have the 2 parts you need.
list($date, $time) = explode(' ', $row_team['datetime_info']);
Field of type date stores the date information, meaning year, month and day (in that order).
time field, as you might have guessed, stores time information in format of hour:minute:second.
If you take a look at what you did, which is inserting a datetime into date field and time field respectively - MySQL doesn't recognize the format (you are passing the data in the wrong format that's expected for date or time field), hence it stores it as default date value.
You need to parse the output of datetime_info column into the date part and time part and then feed it to mysql.

Categories