2017-04-10 143 views

回答

0

PHP已经有日期格式的功能,你需要用strtotime()功能首先转换并与date()

<?php 
$originalDate = "2017/04/11"; 

$theDate = strtotime($originalDate); 

$theDay = date('d',$theDate); 
$theMonth = date('m',$theDate); 
$theYear = date('Y',$theDate); 

$customFormat = date('Y-m-d',$theDate); 
$customFormat2 = date('d/m/Y',$theDate); 
$customFormat3 = date('F j, Y, g:i a',$theDate); 
?> 

例子在这里工作得到所需的值:https://eval.in/772416

您可以获取有关日期的详细信息php here:http://php.net/manual/en/function.date.php

1

您可以使用此方法:

function getDateTimeFromCalendarToFormat($format = 'yyyy-MM-dd HH:mm:ss',  $time = null, $locale = 'fa_IR', $calendar = 'persian', $timeZone = 'Asia/Tehran') 
{ 
    if (empty($time)) { 
     $time = new \DateTime(); 
    } 

    $formatter = new \IntlDateFormatter("{$locale}@calendar={$calendar}", \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $timeZone, \IntlDateFormatter::TRADITIONAL); 
    $dateArray = []; 

    $formatter->setPattern($format); 

    return $formatter->format($time); 
} 

如果你打电话getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'en_US')
返回
1396-06-27 13:50:21

如果你打电话getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'fa_IR')
返回
۱۳۹۶-۰۶-۲۷ ۱۳:۴۹:۵۱