以不同的格式获取日期

时间:2015-03-27 09:09:51

标签: php date

我只想知道如何在这个上获得php日期。 “2015年4月4日至25日”,

此表的结构类似于“date_start = 04 - 04”和“date_end = 04 - 25”和“year = 2015”

此致

2 个答案:

答案 0 :(得分:0)

试试这段代码。

$date_start = "04 - 04";
$date_end = "05 - 25";
$year = "2015";

$date_start = preg_replace('/\s+/', '', $date_start);
$date_end = preg_replace('/\s+/', '', $date_end);

list($month1, $startDate) = explode("-",$date_start);
list($month2, $endDate) = explode("-",$date_end);

if($month1 == $month2)
{
    $month1 =  date("F",strtotime("2015-".$month1."-01"));
    $Output = $month1." ".(int)$startDate." - ".(int)$endDate.", ".$year;
}
else
{
    $month1 =  date("F",strtotime("2015-".$month1."-01"));
    $month2 =  date("F",strtotime("2015-".$month2."-01"));

    $Output = $month1." ".(int)$startDate." - ".$month2. " ". (int)$endDate.", ".$year;
}

echo $Output;

答案 1 :(得分:0)

你在寻找这样的东西:

<?php
$date_start = explode(" - ","04 - 04");
$date_end   = explode(" - ","05 - 25");
$year       = "2015";

$monthNumStart  = $date_start[0];
$monthNumEnd    = $date_end[0];

if($monthNumStart == $monthNumEnd)
{
    $monthName = date('F', mktime(0, 0, 0, $monthNumStart, 10)); 
    $date = $monthName.' '.$date_start[1].' - '.$date_end[1].', '.$year; 
}else{
    $monthName = date('F', mktime(0, 0, 0, $monthNumEnd, 10)); 
    $date = $monthName.' '.$date_start[1].' - '.$date_end[1].', '.$year; 
}
echo $date; 
?>