简体   繁体   English

将8位数的值更改为欧洲日期格式

[英]Change 8 digit value in to a European date format

Using PHP I have data feed fed in to an array: $index->indexDate this gives me a European style 8 digit date (There is no time just the 8 digit date). 使用PHP,我将数据馈入到一个数组中: $index->indexDate这给了我欧式风格的8位日期(没有时间只是8位日期)。

For example: The value 28112012 should result in 28-11-2012 or 28/12/2012 . 例如:值28112012应为28-11-201228/12/2012 I have tried several methods but I am not sure how I should be doing this, I keep ending up with a 1970 date. 我尝试了几种方法,但是我不确定应该怎么做,我一直以1970年的日期告终。

There's the DateTime class, and its format methods 有DateTime类及其format方法

$date = \DateTime::createFromFormat('Ymd', '28112012')->format('d-m-Y')

If your application need to handle full internationalization ("i18n"), then you should take a look at PHP's Intl extension . 如果您的应用程序需要处理完整的国际化(“ i18n”),那么您应该看看PHP的Intl扩展 Basically, it adapt the formatting of numbers, date, currency, etc. for every country. 基本上,它适用于每个国家/地区的数字,日期,货币等格式。

How about using substring ? 如何使用substring link 链接

$date="28112012";
$new_date=substr($date,0,2)."-".substr($date,2,2)."-".substr($date,4,4);

Use - or / . 使用-/

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

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