php数组日期DDMMYY



我有一个表单,当它提交到我的电子邮件时,它显示YYYMDD。我需要它显示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。

使用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

您可以将日期转换为实际日期,然后重新格式化,但我更愿意按照您需要的顺序取出您需要的部分。像这样:

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

这将返回:

280718

这样就不会更改日期函数,也不会因为任何时间设置而以任何方式更改日期,这有时会发生(在您最意想不到的时候(。

如果将字符串拆分为2的一部分,然后反转数组并内爆回字符串——它的日期顺序相反。

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

https://3v4l.org/cZWJk

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

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

最新更新