如何在PHP中定义自己的日期格式?

时间:2017-01-10 08:07:39

标签: php date calendar

你能澄清一下,我如何在PHP中定义自己的日期格式?

例如,我有一个日期变量。

我希望将此日期输出为古罗马日历的日期。或者以法国革命日历的格式。例如,这些日历不是从圣诞节开始的,而是有几个月的其他名称。

有些日历在7天内没有周数,但在另一个天数内有“周”。

或格式为“年份和季节”。例如,“2016年,夏天”。

是否可以在PHP日期格式中添加日期格式并以某种方式编码其输出?

2 个答案:

答案 0 :(得分:1)

class MyDateTime extends DateTime {
      public string format ( $format ) {
             if ($format == "RD") { 
                //Convert to roman date
                return $romanDateFormat;

             }
             return parent::format($format);
      }

}

然后只使用MyDateTime代替DateTime

据我所知,您无法直接为内置DateTime对象添加新格式。

答案 1 :(得分:0)

可以定义自己的功能。 我认为大多数人会这样做的方法是创建一个functions.php文件,您可以在其中存储您的函数和其他自定义函数。 然后在要显示罗马日期的文件中包含functions.php。 然后声明你的功能:

function translateToRomanCalendar($date = ""){//declare function

   $romanDate = "";//declare roman date variable

   //Put the translation code here. Using tests and stuff to store 
   //the  roman date  in the romanDate variable

   return $romanDate;//return the roman date

}

然后在php文件中调用该函数:例如,显示你将要翻译的当前年份

<?php echo translateToRomanCalendar(date('Y')) ?>

这将回显函数的返回值($ romanDate&#39; s)。

现在实际功能的代码是多么棘手;你必须测试日期格式和东西。

无论如何希望有助于网上有大量有关php功能的资料。

此致

亚历