将日期格式(在 DB 或输出中)更改为 dd/mm/yyyy - PHP MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2480186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
提问by Jess
MySQL stores the date in my database (by default) as 'YYYY-MM-DD' The field type for my date is 'DATE' (I do not need any time storage).. Is there a simple way to change it by default to DD/MM/YYYY ?
MySQL 将我的数据库中的日期(默认情况下)存储为 'YYYY-MM-DD' 我的日期的字段类型是 'DATE'(我不需要任何时间存储)。是否有一种简单的方法可以默认更改它到 DD/MM/YYYY ?
I call up different dates in 2 different tables, and no where in any of my code do I have anything resembling a date variable or anything! Hopefully this is a straight forward change?
我在 2 个不同的表中调用了不同的日期,并且在我的任何代码中都没有任何类似于日期变量或任何东西的东西!希望这是一个直接的改变?
回答by Pascal MARTIN
In PHP, you could :
在 PHP 中,你可以:
A bit like this, I'd say :
有点像这样,我会说:
$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);
But this will only work for dates between 1970 and 2038, as timestampsare stored as 32 bits integers, counting from 1970-01-01.
但这仅适用于 1970 年和 2038 年之间的日期,因为时间戳存储为 32 位整数,从 1970-01-01 开始计数。
In MySQL, I suppose the date_formatfunction would do the trick.
For example :
在 MySQL 中,我想该date_format函数可以解决问题。
例如 :
mysql> select date_format(curdate(), '%d/%m/%Y');
+------------------------------------+
| date_format(curdate(), '%d/%m/%Y') |
+------------------------------------+
| 19/03/2010 |
+------------------------------------+
1 row in set (0.03 sec)
And, for the sake of completness, another solution, in PHP, that doesn't suffer from the limitation of 1970-2038 would be to use the DateTimeclass, and, especially :
而且,为了完整起见,在 PHP 中不受 1970-2038 限制的另一种解决方案是使用DateTime该类,尤其是:
DateTime::__constructto parse the date returned by the DBDateTime::formatto format the date to whatever format you want.
DateTime::__construct解析数据库返回的日期DateTime::format将日期格式化为您想要的任何格式。
For example, this portion of code :
例如,这部分代码:
$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');
would get you this output :
会给你这个输出:
19/03/2010
回答by John Conde
Just use the Mysql built in function DATE_FORMAT()
只需使用 Mysql 内置函数DATE_FORMAT()
SELECT DATE_FORMAT(some_date_field, "Y/m/d");
回答by D.Martin
You can display you date in any format you want in your pages, in mysql i realy don't know, in php you can use this function: date ( string $format [, int $timestamp ] ). So you can do this:
你可以在你的页面中以任何你想要的格式显示你的日期,在 mysql 我真的不知道,在 php 中你可以使用这个函数: date ( string $format [, int $timestamp ] )。所以你可以这样做:
echo date( "d/m/Y", strtotime ( $your_date ) );
echo date("d/m/Y", strtotime ( $your_date ) );
You can find full description here: http://php.net/manual/en/function.date.php
你可以在这里找到完整的描述:http: //php.net/manual/en/function.date.php
回答by Please_Dont_Bully_Me_SO_Lords
If you used some Framework like Zend, it would be very easy because you have plenty of classes to handle databases and date formatting. Then you would always see the output as the Brazilian date format. Just like Delphi TDateField uses Windows date format in the current computer, Zend will look up in your own configuration.
如果您使用像 Zend 这样的框架,那将非常容易,因为您有大量的类来处理数据库和日期格式。然后您将始终看到输出为巴西日期格式。就像 Delphi TDateField 在当前计算机中使用 Windows 日期格式一样,Zend 会在您自己的配置中查找。
回答by Nurfaezah Hanis
i'm using this script and put it on upper line.
我正在使用这个脚本并将它放在上面。
$(function(){
var pickerOpts = {
dateFormat: "yy-mm-dd"
};
$("#datepicker").datepicker(pickerOpts);
});
and this one in your form.
和这个在你的形式。
<input id=**"datepicker" type="date"** name="Start" size="9" value="<?php
echo $Start;
?>" />
It will appear as d/m/y on your page but in your database y/m/d.
它将在您的页面上显示为 d/m/y,但在您的数据库中显示为 y/m/d。
回答by St. John Johnson
After getting it from the DB, use $time = strtotime($value)to convert it to a timestamp in PHP. Then use date("d/m/Y", $time)to get it in that format.
从数据库中获取后,使用$time = strtotime($value)PHP 将其转换为时间戳。然后使用date("d/m/Y", $time)以该格式获取它。
回答by Nandhini
This mysql function will return the correct format in the result
这个 mysql 函数将在结果中返回正确的格式
SELECT DATE_FORMAT(date_field, "%M %d %Y");
回答by Keval Mehta
$dtShowDate = $_POST['dtShowDate'];
$date =explode('-', $dtShowDate)0;
$showdate = $date[2]."-".$date[0]."-".$date[1];
$dtShowDate = $_POST['dtShowDate'];
$date =explode('-', $dtShowDate)0;
$showdate = $date[2]."-".$date[0]."-".$date[1];
Set Date as per your need
根据您的需要设置日期

