更改,翻译日期格式 [英] change, translate date format

查看:271
本文介绍了更改,翻译日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个简单的问题。
我有一个函数,我将一个日期传递为意大利格式,我想将格式更改为英文。



我已经尝试过:

  public function changeDateFormat($ date_start_old){
$ date_start_old = strtotime($ date_start_old);
$ date_start = date(Y / m / d,$ date_start_old);

echo('Date start:'。$ date_start);
}

我的旧date_start是:

  01 / lug / 2013 

的01/07/2013)



当我尝试打印新的date_start时,我检索到这是因为不认识 lug

  1970/01/01 

是否有一种方式可以将意大利日期转换为英文和更改格式?



谢谢

解决方案

在您的情况下,我将使用一个简单的翻译数组来将意大利的月份名称转换为英文月份名称。这样的东西应该可以工作: -

  / ** 
* @param $ dateStr
* @return DateTime
* /
函数getDateFromItalian($ dateStr)
{
$ translation = array(
'gennaio'=>'January',
' febbraio'=>'二月',
'marzo'=>'三月',
'aprile'=>'四月',
'maggio'=& ,
'giugno'=>'June',
'luglio'=>'七月',
'agosto'=>'八月',
'settembre' =''九月',
'ottobre'=>'十月',
'novembre'=>'十一月',
'dicembre'=>'十二月' b $ b'gen'=>'January',
'feb'=>'二月',
'mar'=>'三月',
'apr'=> ;'4月',
'mag'=>'May',
' giu'=>'June',
'lug'=>'July',
'ago'=>'August',
'set'=& ,
'ott'=> '十月',
'nov'=> '11月',
'dic'=> '十二月',
);

list($ day,$ month,$ year)= explode('/',$ dateStr);
return new \DateTime($ day。'$。$ translation [strtolower($ month)]。''。$ year,new \DateTimeZone('Europe / Rome'))
}

var_dump(getDateFromItalian('01 / lug / 2013'));

输出: -

  object(DateTime)[1] 
public'date'=> string'2013-07-01 00:00:00'(length = 19)
public'timezone_type'=> int 3
public'timezone'=> string'Europe / Rome'(length = 11)

请参阅 $ p

I have a simple problem into my code. I have a function where I pass a date into an italian format and I want to change the format to english.

I have try this:

public function changeDateFormat($date_start_old){
        $date_start_old = strtotime($date_start_old);
        $date_start = date("Y/m/d" , $date_start_old);

        echo('Date start: '.$date_start);
}

My old date_start is:

01/lug/2013

(is equivalent of 01/07/2013)

And when I try to print new date_start I retrieve this because doesn't recognize lug:

1970/01/01 

Is there a way to transform italian date to english and after change format?

Thanks

解决方案

In your case I would use a simple translation array to convert Italian month names to English month names. Something like this should work:-

/**
 * @param $dateStr
 * @return DateTime
 */
function getDateFromItalian($dateStr)
{
    $translation = array(
        'gennaio' => 'January',
        'febbraio' => 'February',
        'marzo' => 'March',
        'aprile' => 'April',
        'maggio' => 'May',
        'giugno' => 'June',
        'luglio' => 'July',
        'agosto' => 'August',
        'settembre' => 'September',
        'ottobre' => 'October',
        'novembre' => 'November',
        'dicembre' => 'December',
        'gen' => 'January',
        'feb' => 'February',
        'mar' => 'March',
        'apr' =>    'April',
        'mag' => 'May',
        'giu' => 'June',
        'lug' => 'July',
        'ago' => 'August',
        'set' => 'September',
        'ott' => 'October',
        'nov' => 'November',
        'dic' => 'December',
    );

    list($day, $month, $year) = explode('/', $dateStr);
    return new \DateTime($day . ' ' . $translation[strtolower($month)] . ' ' . $year, new \DateTimeZone('Europe/Rome'));
}

var_dump(getDateFromItalian('01/lug/2013'));

Output:-

object(DateTime)[1]
  public 'date' => string '2013-07-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Rome' (length=11)

see the PHP DateTime manual for more information.

这篇关于更改,翻译日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆