我的PHP时差功能可以改进吗?(Can my PHP time difference function be improved?)

编程入门 行业动态 更新时间:2024-10-12 16:27:37
我的PHP时差功能可以改进吗?(Can my PHP time difference function be improved?)

下面是我的函数,它将采用时间戳并告诉你从现在开始的时间,格式为23天3小时4分6秒

主要问题是在我的网站上我使用mysql的DATETIME而不是TIMESTAMP所以要使用这个函数我必须将我的日期时间从mysql转换为时间戳,然后通过我的函数运行它。

所以我很好奇,有没有更好的方法来做到这一点,在我有100个mysql结果的一些页面上,PHP必须将100个日期转换为时间戳,然后在其中100个上运行它。

我只是想知道是否有更好的性能方法,请不要推荐所有PHP框架(zend等)

感谢任何提示/帮助

function duration($timestamp) { $years = floor($timestamp / (60 * 60 * 24 * 365)); $timestamp %= 60 * 60 * 24 * 365; $weeks = floor($timestamp / (60 * 60 * 24 * 7)); $timestamp %= 60 * 60 * 24 * 7; $days = floor($timestamp / (60 * 60 * 24)); $timestamp %= 60 * 60 * 24; $hrs = floor($timestamp / (60 * 60)); $timestamp %= 60 * 60; $mins = floor($timestamp / 60); $secs = $timestamp % 60; $str = ""; if ($years == 1) { $str .= "{$years} year "; }elseif ($years > 1) { $str .= "{$years} yearss "; } if ($weeks == 1) { $str .= "{$weeks} week "; }elseif ($weeks > 1) { $str .= "{$weeks} weeks "; } if ($days == 1) { $str .= "{$days} day "; }elseif ($days > 1) { $str .= "{$days} days "; } if ($hrs == 1) { $str .= "{$hrs} hour "; }elseif ($hrs > 1) { $str .= "{$hrs} hours "; } if ($mins == 1) { $str .= "{$mins} minute "; }elseif ($mins > 1) { $str .= "{$mins} minutes "; } if ($mins < 1 && $secs >= 1) { $str .= "{$secs} seconds "; } return $str; }

Below is my function that will take a timestamp and tell you the time that has passed from now in the format of 23 days 3 hours 4 minutes 6 seconds

The main problem is on my site I use mysql's DATETIME instead of TIMESTAMP so to use this function I must convert my datetime from mysql to a timestamp and then run it through my function.

So I am curious, is there a better way to do this, on some pages where I have 100 mysql results, PHP must convert 100 dates to timestamps and then run this on 100 of them.

I am just wondering if there is a better performance method, and please don't recommend all the PHP frameworks (zend, etc.)

Appreciate any tips/help

function duration($timestamp) { $years = floor($timestamp / (60 * 60 * 24 * 365)); $timestamp %= 60 * 60 * 24 * 365; $weeks = floor($timestamp / (60 * 60 * 24 * 7)); $timestamp %= 60 * 60 * 24 * 7; $days = floor($timestamp / (60 * 60 * 24)); $timestamp %= 60 * 60 * 24; $hrs = floor($timestamp / (60 * 60)); $timestamp %= 60 * 60; $mins = floor($timestamp / 60); $secs = $timestamp % 60; $str = ""; if ($years == 1) { $str .= "{$years} year "; }elseif ($years > 1) { $str .= "{$years} yearss "; } if ($weeks == 1) { $str .= "{$weeks} week "; }elseif ($weeks > 1) { $str .= "{$weeks} weeks "; } if ($days == 1) { $str .= "{$days} day "; }elseif ($days > 1) { $str .= "{$days} days "; } if ($hrs == 1) { $str .= "{$hrs} hour "; }elseif ($hrs > 1) { $str .= "{$hrs} hours "; } if ($mins == 1) { $str .= "{$mins} minute "; }elseif ($mins > 1) { $str .= "{$mins} minutes "; } if ($mins < 1 && $secs >= 1) { $str .= "{$secs} seconds "; } return $str; }

最满意答案

请查看PHP站点上 time函数的文档。 特别是这个和这个 。

以下是Aidan Lister的一个类似片段:

/** * A function for making time periods readable * * @author Aidan Lister <aidan@php.net> * @version 2.0.0 * @link http://aidanlister.com/2004/04/making-time-periods-readable/ * @param int number of seconds elapsed * @param string which time periods to display * @param bool whether to show zero time periods */ function time_duration($seconds, $use = null, $zeros = false) { // Define time periods $periods = array ( 'years' => 31556926, 'Months' => 2629743, 'weeks' => 604800, 'days' => 86400, 'hours' => 3600, 'minutes' => 60, 'seconds' => 1 ); // Break into periods $seconds = (float) $seconds; foreach ($periods as $period => $value) { if ($use && strpos($use, $period[0]) === false) { continue; } $count = floor($seconds / $value); if ($count == 0 && !$zeros) { continue; } $segments[strtolower($period)] = $count; $seconds = $seconds % $value; } // Build the string foreach ($segments as $key => $value) { $segment_name = substr($key, 0, -1); $segment = $value . ' ' . $segment_name; if ($value != 1) { $segment .= 's'; } $array[] = $segment; } $str = implode(', ', $array); return $str; }

Do take a look at the documentation for the time function at the PHP site. Specially this and this.

Here is a snippet by Aidan Lister that is similar:

/** * A function for making time periods readable * * @author Aidan Lister <aidan@php.net> * @version 2.0.0 * @link http://aidanlister.com/2004/04/making-time-periods-readable/ * @param int number of seconds elapsed * @param string which time periods to display * @param bool whether to show zero time periods */ function time_duration($seconds, $use = null, $zeros = false) { // Define time periods $periods = array ( 'years' => 31556926, 'Months' => 2629743, 'weeks' => 604800, 'days' => 86400, 'hours' => 3600, 'minutes' => 60, 'seconds' => 1 ); // Break into periods $seconds = (float) $seconds; foreach ($periods as $period => $value) { if ($use && strpos($use, $period[0]) === false) { continue; } $count = floor($seconds / $value); if ($count == 0 && !$zeros) { continue; } $segments[strtolower($period)] = $count; $seconds = $seconds % $value; } // Build the string foreach ($segments as $key => $value) { $segment_name = substr($key, 0, -1); $segment = $value . ' ' . $segment_name; if ($value != 1) { $segment .= 's'; } $array[] = $segment; } $str = implode(', ', $array); return $str; }

更多推荐

本文发布于:2023-08-04 21:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时差   功能   PHP   time   improved

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!