简体   繁体   English

PHP交换时间戳记中的月份和日期

[英]PHP swap month and day in timestamp

Right. 对。 I was inserting a load of data into a MySQL DB and used the following to generate the timestamp: 我正在将大量数据插入MySQL数据库,并使用以下代码生成时间戳:

$stamp = mktime($t[0], $t[1], $t[2], $d[2], $d[1], $d[0]);

Unfortunately, the day and month were mixed around, and below is the correct timestamp. 不幸的是,日期和月份混杂在一起,下面是正确的时间戳。

$stamp = mktime($t[0], $t[1], $t[2], $d[1], $d[2], $d[0]);

It is about 5,000 records. 大约有5,000条记录。 What is the easiest way to do a bulk update and correction? 进行批量更新和更正的最简单方法是什么?

Thanks a lot! 非常感谢!

Did the database engine allow you to insert impossible dates, or didn't you have any date with the Day field > 12? 数据库引擎是否允许您插入不可能的日期,或者您没有“日期”字段> 12的任何日期?

In any case, you can probably fix it with one update statement, but the syntax is dependant on the database engine you use. 在任何情况下,您都可以使用一条update语句对其进行修复,但是语法取决于您使用的数据库引擎。

For MySQL you would use: 对于MySQL,您可以使用:

UPDATE myTable SET dateColumn = STR_TO_DATE(DATE_FORMAT(dateColumn, '%d-%c-%Y %T'), '%c-%d-%Y %T')

You can't. 你不能 By way of explanation, I will give an example: 作为解释,我将举一个例子:

Suppose you meant: Day 13, month 2, year 1990 But this was read as: Day 2, month 13, year 1990 Since the months in a year only go up to 12, this would be treated as: Day 2, month 1, year 1991 假设您的意思是:1990年第2个月的第13天,但这被理解为:1990年第2个月的第13天,因为一年中的月份最多增加到12个,所以将其视为:第2天,第1个月1991年

But suppose you meant: Day 1, month 2, year 1991 This would have been read as: Day 2, month 1, year 1991 但假设您的意思是:1991年第2个月的第1天,这应该被理解为:1991年第1个月的第2天,

So if you see day 2, month 1, year 1991 in the database, which did it come from? 因此,如果您在数据库中看到1991年的第2天,第1个月,它是从哪来的?

That's like saying, I squared an integer, and the result is 36. Which did I start with, -6 or 6 ? 就像说,我对一个整数求平方,结果是36。我从-6或6开始?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM