Why this datetime to timestamp convertion in PHP doesn't work?
I want to convert this format of datetime "31-12-2018 19:30 hs." from Argentina to an UTC timestamp, I am using the following code:
$clean_date = substr($date, 0, -4);
$dt = new DateTime($clean_date, new DateTimeZone('America/Argentina/Buenos_Aires'));
$dt->setTimeZone(new DateTimeZone('UTC'));
$timestamp = $dt->getTimestamp();
But it doesn't work, in the database the record is "0000-00-00 00:00:00", but if I echo the $dt, till there is working perfectly and showing the datetime in UTC.
Could someone please help me?
Thanks.
php mysql datetime timestamp
add a comment |
I want to convert this format of datetime "31-12-2018 19:30 hs." from Argentina to an UTC timestamp, I am using the following code:
$clean_date = substr($date, 0, -4);
$dt = new DateTime($clean_date, new DateTimeZone('America/Argentina/Buenos_Aires'));
$dt->setTimeZone(new DateTimeZone('UTC'));
$timestamp = $dt->getTimestamp();
But it doesn't work, in the database the record is "0000-00-00 00:00:00", but if I echo the $dt, till there is working perfectly and showing the datetime in UTC.
Could someone please help me?
Thanks.
php mysql datetime timestamp
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.
– Leandro
Jan 1 at 9:51
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06
add a comment |
I want to convert this format of datetime "31-12-2018 19:30 hs." from Argentina to an UTC timestamp, I am using the following code:
$clean_date = substr($date, 0, -4);
$dt = new DateTime($clean_date, new DateTimeZone('America/Argentina/Buenos_Aires'));
$dt->setTimeZone(new DateTimeZone('UTC'));
$timestamp = $dt->getTimestamp();
But it doesn't work, in the database the record is "0000-00-00 00:00:00", but if I echo the $dt, till there is working perfectly and showing the datetime in UTC.
Could someone please help me?
Thanks.
php mysql datetime timestamp
I want to convert this format of datetime "31-12-2018 19:30 hs." from Argentina to an UTC timestamp, I am using the following code:
$clean_date = substr($date, 0, -4);
$dt = new DateTime($clean_date, new DateTimeZone('America/Argentina/Buenos_Aires'));
$dt->setTimeZone(new DateTimeZone('UTC'));
$timestamp = $dt->getTimestamp();
But it doesn't work, in the database the record is "0000-00-00 00:00:00", but if I echo the $dt, till there is working perfectly and showing the datetime in UTC.
Could someone please help me?
Thanks.
php mysql datetime timestamp
php mysql datetime timestamp
edited Jan 1 at 10:48
Álvaro González
106k30187277
106k30187277
asked Jan 1 at 9:16
LeandroLeandro
266
266
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.
– Leandro
Jan 1 at 9:51
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06
add a comment |
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.
– Leandro
Jan 1 at 9:51
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:
$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.– Leandro
Jan 1 at 9:51
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:
$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.– Leandro
Jan 1 at 9:51
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06
add a comment |
2 Answers
2
active
oldest
votes
This has nothing to do with PHP. You're simply using the incorrect date literal format in MySQL. A per the docs:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any
punctuation character may be used as the delimiter between date parts
or time parts.
As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a
date.
As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date.
1546335960
could be the last case but numbers don't make sense as date because year 1546 did not have 33 months.
To make it worse, many MySQL Servers are configured by default to let these kind of errors slip through:
mysql> CREATE TABLE test (
-> foo TIMESTAMP
-> );
Query OK, 0 rows affected (0.74 sec)
mysql> SET @@SESSION.sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'foo' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+---------------------+
| foo |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
As you can see, you got a mere warning (that you need to read explicitly) and data corruption.
If you configure your app to use a strict mode you'll get a proper error message just in time:
mysql> SET @@SESSION.sql_mode = 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
ERROR 1292 (22007): Incorrect datetime value: '1546335960' for column 'foo' at row 1
mysql>
Please note that timestamp is just a generic English word:
A digital record of the time of occurrence of a particular event.
It isn't necessarily synonym for Unix time.
You don't think that is Unix?1546335960
3v4l.org/OeNtB
– Andreas
Jan 1 at 10:52
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
add a comment |
You must specify the date/time as one of these formats:
$date = "31-12-2018 19:30 hs.";
$clean_date = substr($date, 0, -4);
// explicitly specify the format instead of making PHP guess
$dt = DateTime::createFromFormat("d-m-Y H:i", $clean_date, new DateTimeZone("America/Argentina/Buenos_Aires"));
$dt->setTimeZone(new DateTimeZone("UTC"));
$date_str = $dt->format("Y-m-d H:i:s");
// 2018-12-31 22:30:00
Having said that, if you're converting timezones in PHP code you could simply use the DATETIME
data type. See the documentation about how MySQL handles TIMESTAMP
values and what happens if server timezone changes.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994279%2fwhy-this-datetime-to-timestamp-convertion-in-php-doesnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This has nothing to do with PHP. You're simply using the incorrect date literal format in MySQL. A per the docs:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any
punctuation character may be used as the delimiter between date parts
or time parts.
As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a
date.
As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date.
1546335960
could be the last case but numbers don't make sense as date because year 1546 did not have 33 months.
To make it worse, many MySQL Servers are configured by default to let these kind of errors slip through:
mysql> CREATE TABLE test (
-> foo TIMESTAMP
-> );
Query OK, 0 rows affected (0.74 sec)
mysql> SET @@SESSION.sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'foo' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+---------------------+
| foo |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
As you can see, you got a mere warning (that you need to read explicitly) and data corruption.
If you configure your app to use a strict mode you'll get a proper error message just in time:
mysql> SET @@SESSION.sql_mode = 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
ERROR 1292 (22007): Incorrect datetime value: '1546335960' for column 'foo' at row 1
mysql>
Please note that timestamp is just a generic English word:
A digital record of the time of occurrence of a particular event.
It isn't necessarily synonym for Unix time.
You don't think that is Unix?1546335960
3v4l.org/OeNtB
– Andreas
Jan 1 at 10:52
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
add a comment |
This has nothing to do with PHP. You're simply using the incorrect date literal format in MySQL. A per the docs:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any
punctuation character may be used as the delimiter between date parts
or time parts.
As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a
date.
As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date.
1546335960
could be the last case but numbers don't make sense as date because year 1546 did not have 33 months.
To make it worse, many MySQL Servers are configured by default to let these kind of errors slip through:
mysql> CREATE TABLE test (
-> foo TIMESTAMP
-> );
Query OK, 0 rows affected (0.74 sec)
mysql> SET @@SESSION.sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'foo' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+---------------------+
| foo |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
As you can see, you got a mere warning (that you need to read explicitly) and data corruption.
If you configure your app to use a strict mode you'll get a proper error message just in time:
mysql> SET @@SESSION.sql_mode = 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
ERROR 1292 (22007): Incorrect datetime value: '1546335960' for column 'foo' at row 1
mysql>
Please note that timestamp is just a generic English word:
A digital record of the time of occurrence of a particular event.
It isn't necessarily synonym for Unix time.
You don't think that is Unix?1546335960
3v4l.org/OeNtB
– Andreas
Jan 1 at 10:52
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
add a comment |
This has nothing to do with PHP. You're simply using the incorrect date literal format in MySQL. A per the docs:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any
punctuation character may be used as the delimiter between date parts
or time parts.
As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a
date.
As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date.
1546335960
could be the last case but numbers don't make sense as date because year 1546 did not have 33 months.
To make it worse, many MySQL Servers are configured by default to let these kind of errors slip through:
mysql> CREATE TABLE test (
-> foo TIMESTAMP
-> );
Query OK, 0 rows affected (0.74 sec)
mysql> SET @@SESSION.sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'foo' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+---------------------+
| foo |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
As you can see, you got a mere warning (that you need to read explicitly) and data corruption.
If you configure your app to use a strict mode you'll get a proper error message just in time:
mysql> SET @@SESSION.sql_mode = 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
ERROR 1292 (22007): Incorrect datetime value: '1546335960' for column 'foo' at row 1
mysql>
Please note that timestamp is just a generic English word:
A digital record of the time of occurrence of a particular event.
It isn't necessarily synonym for Unix time.
This has nothing to do with PHP. You're simply using the incorrect date literal format in MySQL. A per the docs:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any
punctuation character may be used as the delimiter between date parts
or time parts.
As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a
date.
As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date.
1546335960
could be the last case but numbers don't make sense as date because year 1546 did not have 33 months.
To make it worse, many MySQL Servers are configured by default to let these kind of errors slip through:
mysql> CREATE TABLE test (
-> foo TIMESTAMP
-> );
Query OK, 0 rows affected (0.74 sec)
mysql> SET @@SESSION.sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'foo' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+---------------------+
| foo |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
As you can see, you got a mere warning (that you need to read explicitly) and data corruption.
If you configure your app to use a strict mode you'll get a proper error message just in time:
mysql> SET @@SESSION.sql_mode = 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test (foo) VALUES (1546335960);
ERROR 1292 (22007): Incorrect datetime value: '1546335960' for column 'foo' at row 1
mysql>
Please note that timestamp is just a generic English word:
A digital record of the time of occurrence of a particular event.
It isn't necessarily synonym for Unix time.
edited Jan 1 at 10:58
answered Jan 1 at 10:41
Álvaro GonzálezÁlvaro González
106k30187277
106k30187277
You don't think that is Unix?1546335960
3v4l.org/OeNtB
– Andreas
Jan 1 at 10:52
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
add a comment |
You don't think that is Unix?1546335960
3v4l.org/OeNtB
– Andreas
Jan 1 at 10:52
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
You don't think that is Unix?
1546335960
3v4l.org/OeNtB– Andreas
Jan 1 at 10:52
You don't think that is Unix?
1546335960
3v4l.org/OeNtB– Andreas
Jan 1 at 10:52
1
1
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
@Andreas Yes, it is. But nowhere in MySQL documentation do they say that Unix timestamp is a valid format. You actually need to call a function to preprocess the value.
– Álvaro González
Jan 1 at 10:55
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
Incredible, more than helpful @Álvaro González, I will work on this and when I make it work I will give you the check! THANK you a lot.
– Leandro
Jan 1 at 11:12
add a comment |
You must specify the date/time as one of these formats:
$date = "31-12-2018 19:30 hs.";
$clean_date = substr($date, 0, -4);
// explicitly specify the format instead of making PHP guess
$dt = DateTime::createFromFormat("d-m-Y H:i", $clean_date, new DateTimeZone("America/Argentina/Buenos_Aires"));
$dt->setTimeZone(new DateTimeZone("UTC"));
$date_str = $dt->format("Y-m-d H:i:s");
// 2018-12-31 22:30:00
Having said that, if you're converting timezones in PHP code you could simply use the DATETIME
data type. See the documentation about how MySQL handles TIMESTAMP
values and what happens if server timezone changes.
add a comment |
You must specify the date/time as one of these formats:
$date = "31-12-2018 19:30 hs.";
$clean_date = substr($date, 0, -4);
// explicitly specify the format instead of making PHP guess
$dt = DateTime::createFromFormat("d-m-Y H:i", $clean_date, new DateTimeZone("America/Argentina/Buenos_Aires"));
$dt->setTimeZone(new DateTimeZone("UTC"));
$date_str = $dt->format("Y-m-d H:i:s");
// 2018-12-31 22:30:00
Having said that, if you're converting timezones in PHP code you could simply use the DATETIME
data type. See the documentation about how MySQL handles TIMESTAMP
values and what happens if server timezone changes.
add a comment |
You must specify the date/time as one of these formats:
$date = "31-12-2018 19:30 hs.";
$clean_date = substr($date, 0, -4);
// explicitly specify the format instead of making PHP guess
$dt = DateTime::createFromFormat("d-m-Y H:i", $clean_date, new DateTimeZone("America/Argentina/Buenos_Aires"));
$dt->setTimeZone(new DateTimeZone("UTC"));
$date_str = $dt->format("Y-m-d H:i:s");
// 2018-12-31 22:30:00
Having said that, if you're converting timezones in PHP code you could simply use the DATETIME
data type. See the documentation about how MySQL handles TIMESTAMP
values and what happens if server timezone changes.
You must specify the date/time as one of these formats:
$date = "31-12-2018 19:30 hs.";
$clean_date = substr($date, 0, -4);
// explicitly specify the format instead of making PHP guess
$dt = DateTime::createFromFormat("d-m-Y H:i", $clean_date, new DateTimeZone("America/Argentina/Buenos_Aires"));
$dt->setTimeZone(new DateTimeZone("UTC"));
$date_str = $dt->format("Y-m-d H:i:s");
// 2018-12-31 22:30:00
Having said that, if you're converting timezones in PHP code you could simply use the DATETIME
data type. See the documentation about how MySQL handles TIMESTAMP
values and what happens if server timezone changes.
edited Jan 1 at 11:07
answered Jan 1 at 10:59
Salman ASalman A
181k66339432
181k66339432
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994279%2fwhy-this-datetime-to-timestamp-convertion-in-php-doesnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think this should help you.Click Here
– Yash
Jan 1 at 9:21
And $timestamp is also correct if you echo ? Your field in database use timestamp or datetime ? If its datetime, you need to convert your timestamp back to datetime: $value_for_database = date('Y-m-d H:i:s', $timestamp);
– Steffen Mächtel
Jan 1 at 9:30
The code that inserts data into MySQL could be broken. You need to post those details, too.
– Salman A
Jan 1 at 9:33
The $timestamp echo prints something like this: "1546335960", and the field type in database is timestamp, and the code that I'm using to update the row is this one:
$sql = "UPDATE $tblname SET nombre='$name', creado='$timestamp', msg='$msg' WHERE id=$rowid";
, obviously the other fields are being updated without problems.– Leandro
Jan 1 at 9:51
Is it always Argentina to UTC you want to convert between? If year then it's always the same number of hours + DST
– Andreas
Jan 1 at 10:06