PHP: producing relative date/time from timestamps

I'm basically trying to convert a Unix timestamp (the time() function) to a relative date/time that's both compatible with past and future date. So outputs could be: 2 weeks ago 1 hour and 60 minutes ago 15 minutes and 54 seconds ago after 10 minutes and 15 seconds First I tried to code this, but made a huge unmaintainable function, and then I searched the internet for a couple

PHP:从时间戳生成相对日期/时间

我基本上试图将Unix时间戳(time()函数)转换为与过去和未来日期兼容的相对日期/时间。 所以产出可以是: 2个星期前 1小时和60分钟前 15分54秒前 10分钟和15秒后 首先,我尝试编码,但做了一个巨大的不可维护的功能,然后我搜索了几个小时的互联网,但我能找到的只是一部分时间的脚本(eh:“1小时前”,没有纪要)。 你有脚本已经做到了吗? 此功能可以在“现在”和“特定时间戳”之间给出“1小时前”或“明天”结果。 fun

Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...

I am trying to convert a timestamp of the format 2009-09-12 20:57:19 and turn it into something like 3 minutes ago with PHP. I found a useful script to do this, but I think it's looking for a different format to be used as the time variable. The script I'm wanting to modify to work with this format is: function _ago($tm,$rcs = 0) { $cur_tm = time(); $dif = $cur_tm-$tm; $p

将时间戳转换为PHP之前的时间,例如1天前,2天前...

我正在尝试将格式的时间戳2009-09-12 20:57:19转换成类似于3 minutes ago的PHP。 我发现了一个有用的脚本来做到这一点,但我认为它正在寻找一种不同的格式来作为时间变量。 我想要修改以使用这种格式的脚本是: function _ago($tm,$rcs = 0) { $cur_tm = time(); $dif = $cur_tm-$tm; $pds = array('second','minute','hour','day','week','month','year','decade'); $lngh = array(1,60,3600,86400,604800,

PHP combining arrays

我该如何组合这两个数组,并且如果数组的重复只有一个使用PHP表示。 Array ( [0] => 18 [1] => 20 [2] => 28 [3] => 29 ) Array ( [0] => 1 [1] => 8 [2] => 19 [3] => 22 [4] => 25 [5] => 28 [6] => 30 ) 这听起来像你需要: array_unique(array_merge($first_array, $second_array)); Apply array_unique to the results of the array_merge

组合数组的PHP

我该如何组合这两个数组,并且如果数组的重复只有一个使用PHP表示。 Array ( [0] => 18 [1] => 20 [2] => 28 [3] => 29 ) Array ( [0] => 1 [1] => 8 [2] => 19 [3] => 22 [4] => 25 [5] => 28 [6] => 30 ) 这听起来像你需要: array_unique(array_merge($first_array, $second_array)); 将array_unique应用于array_merge函数的结果。 例: p

PHP: Adding arrays together

Could someone help me explain this? I have two snippets of code, one works as I expect, but the other does not. This works $a = array('a' => 1, 'b' => 2); $b = array('c' => 3); $c = $a + $b; print_r($c); // Output Array ( [a] => 1 [b] => 2 [c] => 3 ) This does not $a = array('a', 'b'); $b = array('c'); $c = $a + $b; print_r($c); // Output Array ( [0] =>

PHP:一起添加数组

有人可以帮我解释一下吗? 我有两个代码片段,其中一个按我期望的方式工作,但另一个没有。 这工作 $a = array('a' => 1, 'b' => 2); $b = array('c' => 3); $c = $a + $b; print_r($c); // Output Array ( [a] => 1 [b] => 2 [c] => 3 ) 这不是 $a = array('a', 'b'); $b = array('c'); $c = $a + $b; print_r($c); // Output Array ( [0] => a [1] => b ) 这里发生了什

Set Theory Union of arrays in PHP

There are 3 operations with sets in mathematics: intersection, difference and union (unification). In PHP we can do this operations with arrays: intersection: array_intersect difference: array_diff What function is for union? No duplicates can be in the result array (like array_intersect and array_diff ). If indexes are numeric then array_merge will not overwrite the original value, bu

在PHP中设置数组的理论联盟

数学中有3组操作:交集,差异和联合(统一)。 在PHP中,我们可以使用数组执行此操作: 交集: array_intersect 差异: array_diff 联合的功能是什么? 结果数组中没有重复项(如array_intersect和array_diff )。 如果索引是数字的,那么array_merge不会覆盖原始值,但会被附加(PHP文档)。 尝试array_merge : array_unique(array_merge($array1, $array2)); PHP手册 array_unique( array_merge( ... ) ) Adr

Can't concatenate 2 arrays in PHP

I've recently learned how to join 2 arrays using the + operator in PHP. But consider this code... $array = array('Item 1'); $array += array('Item 2'); var_dump($array); Output is array(1) { [0]=> string(6) "Item 1" } Why does this not work? Skipping the shorthand and using $array = $array + array('Item 2') does not work either. Does it have something to do wit

无法连接PHP中的2个数组

我最近学会了如何使用PHP中的+运算符连接2个数组。 但考虑这个代码... $array = array('Item 1'); $array += array('Item 2'); var_dump($array); 输出是 array(1){[0] => string(6)“Item 1”} 为什么这不起作用? 跳过简写并使用$array = $array + array('Item 2')也不起作用。 它与钥匙有什么关系? 两者都有一个0的密钥,并且组合这些数组的方法会折叠重复。 尝试使用array_merge()代替。 $arr1

What's the difference between array

A fairly simple question. What's the difference between: $merged = array_merge($array1, $array2); and $merged = $array1 + $array2; ? The difference is: The + operator takes the union of the two arrays, whereas the array_merge function takes the union BUT the duplicate keys are overwritten). Here's a simple illustrative test: $ar1 = [ 0 => '1-0', 'a' => '1-a', 'b'

数组有什么区别

一个相当简单的问题。 有什么区别: $merged = array_merge($array1, $array2); 和 $merged = $array1 + $array2; ? 区别在于: +运算符接受两个数组的联合,而array_merge函数接受联合但重复键被覆盖)。 这是一个简单的说明性测试: $ar1 = [ 0 => '1-0', 'a' => '1-a', 'b' => '1-b' ]; $ar2 = [ 0 => '2-0', 1 => '2-1', 'b' => '2-b', 'c' => '2-c' ]; print_r($a

How to insert an item at the beginning of an array in PHP?

I know how to insert it to the end by: $arr[] = $item; But how to insert it to the beginning? Use array_unshift($array, $item); $arr = array('item2', 'item3', 'item4'); array_unshift($arr , 'item1'); print_r($arr); will give you Array ( [0] => item1 [1] => item2 [2] => item3 [3] => item4 ) In case of an associative array or numbered array where you do not want to change th

如何在PHP的数组的开头插入一个项目?

我知道如何通过以下方式将其插入到最后: $arr[] = $item; 但如何将其插入到开始? 使用array_unshift($ array,$ item); $arr = array('item2', 'item3', 'item4'); array_unshift($arr , 'item1'); print_r($arr); 会给你 Array ( [0] => item1 [1] => item2 [2] => item3 [3] => item4 ) 如果您不想更改数组键的关联数组或数组数组: $firstItem = array('foo' => 'bar'); $arr = $firstItem +

PHP append one array to another (not array

How to append one array to another without comparing their keys? $a = array( 'a', 'b' ); $b = array( 'c', 'd' ); At the end it should be: Array( [0]=>a [1]=>b [2]=>c [3]=>d ) If I use something like [] or array_push , it will cause one of these results: Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) ) //or Array( [0]=>c [1]=>d ) It just should be something,

PHP将一个数组附加到另​​一个(而不是数组)

如何将一个数组追加到另一个数组而不进行比较? $a = array( 'a', 'b' ); $b = array( 'c', 'd' ); 最后它应该是: Array( [0]=>a [1]=>b [2]=>c [3]=>d )如果我使用类似[]或array_push东西,会导致其中一个结果: Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) ) //or Array( [0]=>c [1]=>d ) 它应该是一些东西,这样做,但以一种更优雅的方式: foreach ( $b AS $var ) $a[] =

How to add elements to an empty array in PHP?

If I define an array in PHP such as (I don't define its size): $cart = array(); Do I simply add elements to it using the following? $cart[] = 13; $cart[] = "foo"; $cart[] = obj; Don't arrays in PHP have an add method, for example, cart.add(13) ? Both array_push and the method you described will work. <?php $cart = array(); $cart[] = 13; $cart[] = 14; // etc ?> Is the same as

如何将元素添加到PHP中的空数组?

如果我在PHP中定义一个数组,例如(我没有定义它的大小): $cart = array(); 我是否只需使用以下内容向其添加元素? $cart[] = 13; $cart[] = "foo"; $cart[] = obj; 不要PHP中的数组有一个add方法,例如cart.add(13) ? array_push和你描述的方法都可以工作。 <?php $cart = array(); $cart[] = 13; $cart[] = 14; // etc ?> 是相同的: <?php $cart = array(); array_push($cart, 13); array_push($cart, 14