- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将 mysql 时间戳转换为以月、日、小时或分钟为单位的时间。
所以输出看起来像这样:
1 个月前添加/添加:0 小时前/添加:21 分钟前/30 秒前添加
所以我只想要一种时间格式,具体取决于多少分钟或多少小时或多少天等,所以 60 分钟转换为 1 小时前或 24 小时转换为 1 天前,48 小时转换为 2 天前.
到目前为止我有这段代码:
<?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($news['date_added']);
$interval = $datetime1->diff($datetime2);
str_replace('0 hours', '', $variable);
echo $interval->format('%h hours %i minutes');
?>
这会输出以下内容:
添加于 0 小时前 57 分钟前。
有人可以帮助我或告诉我 id 需要做什么才能使格式正确显示吗,我对 php 真的很陌生,我不确定我该怎么做。谢谢。
最佳答案
来自 http://php.net/manual/en/ref.datetime.php
调用该函数时只需将 $precision 更改为 1,并在日期前后添加您想要的任何文本。您必须确保将日期对象转换为时间戳,但这对您来说应该不是问题。
/**
* this code assumes php >= 5.1.0. if using < 5.1, read
* php.net/strtotime and change the condition for checking
* for failure from strtotime()
*/
// $t1, $t2: unix times, or strtotime parseable
// $precision: max number of units to output
// $abbr: if true, use "hr" instead of "hour", etc.
function date_diff ($t1, $t2, $precision = 6, $abbr = false) {
if (preg_match('/\D/', $t1) && ($t1 = strtotime($t1)) === false)
return false;
if (preg_match('/\D/', $t2) && ($t2 = strtotime($t2)) === false)
return false;
if ($t1 > $t2)
list($t1, $t2) = array($t2, $t1);
$diffs = array(
'year' => 0, 'month' => 0, 'day' => 0,
'hour' => 0, 'minute' => 0, 'second' => 0,
);
$abbrs = array(
'year' => 'yr', 'month' => 'mth', 'day' => 'day',
'hour' => 'hr', 'minute' => 'min', 'second' => 'sec'
);
foreach (array_keys($diffs) as $interval) {
while ($t2 >= ($t3 = strtotime("+1 ${interval}", $t1))) {
$t1 = $t3;
++$diffs[$interval];
}
}
$stack = array();
foreach ($diffs as $interval => $num)
$stack[] = array($num, ($abbr ? $abbrs[$interval] : $interval) . ($num != 1 ? 's' : ''));
$ret = array();
while (count($ret) < $precision && ($item = array_shift($stack)) !== null) {
if ($item[0] > 0)
$ret[] = "{$item[0]} {$item[1]}";
}
return implode(', ', $ret);
}
$t1 = 'Feb 4, 2008 12:16:00';
$t2 = 'Jul 3, 2006 16:15:30';
echo date_diff($t1, $t2), "\n",
date_diff($t1, $t2, 3), "\n",
date_diff($t1, $t2, 2, true), "\n";
?>
关于php - 以月、日、分或秒显示相关日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691883/
我正在使用 C#。 我知道我可以用 ToLongDateString() 显示如下内容: Friday, February 27, 2009 我喜欢做的是展示类似的东西: Febru
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
我需要检查是否 DateTime.Now是在每个月的前 3 个工作日(周一至周五)。我还需要提供一个 List有国定假日,应相应处理。 如果 DateTime.Now 是星期六并且是该月的 1 号,则
我想以 2012 年 1 月 1 日 - 2012 年 1 月 31 日的格式显示日期 并获取由 dateranges['jan 1,2012 - jan 31,2012','December 1,2
我有这两个函数可以以正确的格式 (mm-dd-yyyy) 创建一个新字符串,但现在它似乎工作得不太好......当我输入日期 31-03-2013 这是一个有效的日期,它出现在 04-01-2013
我只想在 Fullcalendar 中加载 December 月份,其中 2 个日期启用 2012 年 12 月 14 日、15 日。 var date = new Date(2012, 11); v
我创建了一个convertDate类,用于转换我的时间戳,即Date().sinceReferenceDate(即自2001年1月1日凌晨12:00起)。我有一个问题:当时间戳的日期超过 3 周前时,
我有来自两个来源的数据,我试图在日期/时间上进行匹配。问题是我没有得到匹配。所以我将日期/时间转换为 Julian 并遇到了一些奇怪的事情。 8/11/2015 6:50:22.000 42227
我正在尝试将毫秒转换为如下所示的日期:Oct 04, 2013 .我将毫秒转换为日期对象: var d1 = new Date(milliseconds); 然后输出如下内容: Fri Oct 04
我正在尝试使用 javascript 进行倒计时。但是,我的倒计时只能计算天数、小时数、分钟数和秒数。我还想显示年份和月份。 以下是我的代码: today = new Date();
我有下面的查询不适合我, 这个函数 from_tz(to_timestamp(START_TIME, 'YYYYMMDDHH24MISS'), substr(UTC_TIME_CODE_OFFSET,
例如,我有一列数据如下: 17.14.11 17.15.10 18.21.06 现在是 2017 年 11 月 14 日,我想将其更改为 DateTime 对象,例如: 2017-11-14 2017
$date1 = "2000-01-01"; $date2 = "2011-03-14"; $diff = abs(strtotime($date2) - strtotime($date1)); $y
我有以下代码来验证给定日期格式的日期: val df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); try { val
我有一个包含两个 DateTime 成员的类。 但是,当我尝试将表示日期的“人类可读”值分配给 DateTime 成员时,它拒绝它并显示消息“字符串未被识别为有效的 DateTime”。 失败的代码行
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题已经有答案了: Adding days to a date in Java [duplicate] (6 个回答) 已关闭 6 年前。 如何在 java 中将给定的日期转换为日历格式。 示例初
这个问题已经有答案了: Illegal pattern character 'T' when parsing a date string to java.util.Date (4 个回答) 已关闭 6
我现在在香港。如何获取美国的年、月、日? 我已经尝试过: Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new
我正在尝试使用 php 和 Mysql 开发一个搜索系统,但是您如何搜索:例如当数据库中的日期格式为 2012-03-26(年/月/日)时的三月份 最佳答案 根据列数据的格式,这样的事情可能会起作用:
我是一名优秀的程序员,十分优秀!