[英]How can I get number of days, hours, minutes and second from now to a date-time given in the MySQL database?
[英]How to increment a given time with a given number of minutes and associate the new time to a array list
我需要用給定的分鍾數增加給定的時間,並將該時間與ID的數組列表相關聯。 我需要的一個例子:
$ids_arrays = array(699926900040821, 699926900040822, 699926900040823);
$given_time='20:30';
$given_minutes = '5';
$newarray=array();
我想創建一個像這樣的新數組:
Array (
[699926900040821] => '20:35'
[699926900040822] => '20:40'
[699926900040822] => '20:45'
)
我的代碼:
//$_GET['grupuri']= simple array ,$ids_arrays;
//$_GET['numar_grup']= number of minutes to increment ;
//$_GET['time_grup']=time array;
$ora_grup=array();
$h1=new DateTime($_GET['time_grup']);
$m1=new DateInterval('PT'.$_GET['numar_grup'].'M');
for ( $i=0; $h=$h1; $h->format('H') <10; $i <count($_GET['grupuri']) ; $i++, $h->add($m1)) {
$ora_grup[$_GET['grupuri'][$i]]=$h->format("H:i\n");
}
嘗試這個:
$ids_arrays = array('699926900040821', '699926900040822', '699926900040823');
$given_time = '20:30';
$given_minutes = '5';
$t = strtotime('today '. $given_time);
$newarray = array();
foreach ($ids_arrays as $id) {
$newarray[$id] = date('H:i:s', $t += $given_minutes * 60);
}
這樣可以:
/**
* @return array
*/
function associateIdsWithTime(
array $ids,
DateTime $startTime,
DateInterval $interval
) {
$associatedIds = array();
foreach ($ids as $id) {
$associatedIds[$id] = $startTime->add($interval)->format('H:i');
}
return $associatedIds;
}
然后,您可以這樣稱呼它:
$associatedIds = associateIdsWithTime(
array(699926900040821, 699926900040822, 699926900040823),
DateTime::createFromFormat('H:i', $given_time),
DateInterval::createFromDateString("+ $given_minutes minutes")
);
對於$given_time = 20:30
和$given_minutes = 5
,它將返回:
Array
(
[699926900040821] => 20:35
[699926900040822] => 20:40
[699926900040823] => 20:45
)
附帶一提:請注意32位系統上的ID。 您使用的是整數,但32位系統上的PHP_INT_MAX
限制為2.147.483.647,不能容納699.926.900.040.822。 您可能需要更改為字符串以使其可移植。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.