简体   繁体   English

日期格式 - DD-MM-YYYY 到 YYYY-MM-DD

[英]Date formatting - DD-MM-YYYY to YYYY-MM-DD

MySQL table has 2 columns - startdate and enddate MySQL 表有 2 列 - startdate 和 enddate

They hold dates in this format: 22-01-2016他们以这种格式保存日期:22-01-2016

When I was prototyping, I used MediumText.在进行原型设计时,我使用了 MediumText。

Now, I want to switch to Date format.现在,我想切换到日期格式。 The default date format in PHPMyAdmin is YYYY-MM-DD PHPMyAdmin 中的默认日期格式是 YYYY-MM-DD

If I switch the columns to Date, my data turns into 0000-00-00.如果我将列切换到日期,我的数据将变为 0000-00-00。

Using PHP or MySQL, how do I reformat my data prior to going back into PHPMyAdmin to switch the columns to Date format?使用 PHP 或 MySQL,如何在返回 PHPMyAdmin 以将列切换为日期格式之前重新格式化我的数据?

You can use string to date function to update columns data before changing its structure您可以使用 string to date 函数在更改其结构之前更新列数据

UPDATE table SET dateColumn = STR_TO_DATE(dateColumn, '%d-%m-%Y');

It will convert DD-MM-YYYY formatted dates to YYYY-MM-DD.它会将 DD-MM-YYYY 格式的日期转换为 YYYY-MM-DD。 After that you can just change structure of column to DATE之后,您可以将列的结构更改为DATE

The easiest and most reliable way would be to split up the dates and rearrange them the other way round:最简单和最可靠的方法是拆分日期并反过来重新排列它们:

$parts = explode('-', $olddate);
$newdate = sprintf('%04d-%02d-%02d', $parts[2], $parts[1], $parts[0]);

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

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