时间戳mktime php格式

时间:2014-05-05 09:39:28

标签: php format timestamp mktime

我在下面列出了一个包含事件的php页面,我希望按日期排序。没问题,但是我希望日期格式是“15 / Lug / 1998”而不是“15 / Lug / 1998:00:00:00 -0000”。我发布代码来解决这个问题。在此先感谢您的回复和帮助。

<?php

# For demo - some data that we will test with

$stuff =  <<<COURSES
02 - - [15/Lug/1998:00:00:00 -0000] "<a href="http://www.111.com">Event01</a>"
05 - - [21/Lug/1998:00:00:00 -0000] "<a href="http://www.112.com">Event02</a>"
07 - - [16/Lug/1998:00:00:00 -0000] "<a href="http://www.113.com">Event03</a>"
COURSES;

# Date in yukky format to Unix timestamp
function gdate($record) {
eregi('\[([0-9]{2})/([A-Z]{3})/([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})',
    $record, $gotten);
    $yikes = 1 + (strpos("GenFebMarAprMagGiuLugAgoSetOttNovDic",
            $gotten[2]))/3; # Date "Alpha Triplet" to Month No.
     $when = mktime($gotten[4],$gotten[5],$gotten[6],
            $yikes,$gotten[1],$gotten[3]);
    return $when;
    }

function bydate($left,$right) {
    $whenone = gdate($left);
    $whentwo = gdate($right);
    if ($whenone == $whentwo) return 0;
    return (($whenone < $whentwo) ? -1 : +1);
    }

$records = explode("\n",$stuff);
sort ($records);
$result = implode("<br />",$records);
print "<hr> Normal sort</br>$result";

usort ($records,bydate);
$result = implode("<br />",$records);
print "<hr> User defined sort - by date stamp</br>$result";

?>

1 个答案:

答案 0 :(得分:0)

您的时间戳是通过以下代码生成的

function gdate($record) {
eregi('\[([0-9]{2})/([A-Z]{3})/([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})',
    $record, $gotten);
    $yikes = 1 + (strpos("GenFebMarAprMagGiuLugAgoSetOttNovDic",
            $gotten[2]))/3; # Date "Alpha Triplet" to Month No.
     $when = mktime($gotten[4],$gotten[5],$gotten[6],
            $yikes,$gotten[1],$gotten[3]);
    return $when;
    }

修改如下:

function gdate($record) {
eregi('\[([0-9]{2})/([A-Z]{3})/([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})',
    $record, $gotten);
    $yikes = 1 + (strpos("GenFebMarAprMagGiuLugAgoSetOttNovDic",
            $gotten[2]))/3; # Date "Alpha Triplet" to Month No.
     $time = mktime($gotten[4],$gotten[5],$gotten[6],
            $yikes,$gotten[1],$gotten[3]);
      $when = date('d/M/Y', $time);   //this is solution one, recommended
      $when = substr($time, 0, -15);  //this is solution two
    return $when;
    }

编辑: 保留以前的代码并修改以下部分:

$records = explode("\n",$stuff);

TO:

$rec = str_replace(":00:00:00 -0000", "", $stuff);
$records = explode("\n",$rec);

第二次编辑: 变化:(改变两个地方)

$result = implode("<br />",$records);

要:

$result = implode("<br />",str_replace(":00:00:00 -0000", "", $stuff));