开发者

PHP Date Compiler

I've just written a function in PHP which changes dates to a more human-readable format.

function customDate($input_date){
 $datetime = explode(' ', $input_date);
 $date = explode('-', $date);
 $time = explode(':', $time);
 $current = date('Y-m-d H:i:s');
 $currentdateti开发者_如何学Pythonme = explode(' ', $current);
 $currentdate = explode('-', $current_date);

 if($date[2] == $currentdate[2]) return date('H:i', strtotime($input_date));
 else if($date[2] != $currentdate[2] && $date[0] == $currentdate[0]) return date('F d H:i', strtotime($input_date));
 else if($date[2] = $currentdate[2] && $date[0] != $currentdate[0]) return date('F d Y H:i', strtotime($input_date));
 else return $input_date;
}

This changes dates like 2010-11-06 18:25:55 to look more pretty like 18:25 (if 2010-11-06 is today), 11 November 18:25 (if not today but still in year 2010) or 11 November 2010 18:25 if year 2010 has already passed. But that's in theory, I can't find out why function keeps outputting only hours and minutes with different dates. What may cause the problem?


PHP already supports almost any format you want using the date() function. If need be, combine it with strtotime() to get what you want. It's pretty powerful. For example:

if ( date('Y',strtotime($input_date)) < date('Y',time()) ) {
    echo date('d F Y H:i');
} else {
    echo date('d F H:i');
}
// Oter examples
echo date('Y-m-d', strtotime('last tuesday'));
echo date('Y-m-d', strtotime('3 days ago'));
echo date('Y-m-d', strtotime('+4 weeks'));
echo date('Y-m-d H:iA', strtotime('+2 hours 23 minutes'));
echo date('Y-m-d H:iA', strtotime('+2 hours -23 minutes'));


I think function output the same format because it is more precise but also there is no code written to make it more human readable.

But it is a 2-edge sword. Mac like to use "finished in minutes". Windows like to use "finished in 2 minutes 20 seconds".

Some people may find the phrase "finished in minutes" or "seconds" good enough, but at the same time, if I need to catch the bus, and I don't know whether it is 2 minutes or 7 minutes, then it may be a problem. At the same time, I probably don't need to know "2 minutes 20 seconds" vs "2 minutes 15 seconds".

So similar to your problem... sometimes it is necessary to show the details, sometimes not. For example, to show the time of a concert ticket you purchased, probably the month and maybe even the year is necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜