PHP数组日期DDMMYY

时间:2018-07-28 05:50:36

标签: php date

我有一个表格,当它提交到我的电子邮件时,它显示YYMMDD。 我需要显示DDMMYY。 后端中的代码如下:

// subject of the email
$subject = 'New message from booking form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('pickupdate' => 'Pick Up Date' , 'message' => 'Message');

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

如何获取数组字段pickupdate的DDMMYY。

4 个答案:

答案 0 :(得分:0)

使用date_create_from_format()

$date = date_create_from_format('Y-M-j', '2009-Feb-15');
echo date_format($date, 'Y-m-d');

http://php.net/manual/en/datetime.createfromformat.php

答案 1 :(得分:0)

您可以将日期转换成实际日期,然后重新格式化,但我宁愿只按需要的顺序取出所需的位。像这样:

$oldDate = '180728';
$newDate = substr($oldDate,4,2).substr($oldDate,2,2).substr($oldDate,0,2);
echo $newDate;

这将返回:

280718

这样,日期功能就不会因为任何时间设置而以任何方式更改日期,这有时可能会发生(当您最不期望时)。

答案 2 :(得分:0)

如果将字符串拆分为2的一部分,则反转数组并将其内爆到日期的反转顺序。

$str = "121314";

echo implode("",array_reverse(str_split($str, 2)));
//141312

https://3v4l.org/cZWJk

答案 3 :(得分:-1)

只需在strtotime()中传递日期变量即可。

date("d-m-Y", strtotime("$date"));