Php 从数组重新排列值
我的阵列Php 从数组重新排列值,php,arrays,date,Php,Arrays,Date,我的阵列 <? $date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28', '16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
$currentdate = date('y-m-d');
?>
使用该函数可以按任何方式格式化时间戳:
echo date('d/m/y', strtotime($nextdate));
使用输入格式和输入日期创建一个
DateTime
实例,然后转换为输出格式:
$nextdate = DateTime::createFromFormat('y-m-d', $nextdate)->format('m-d-y');
我认为您的所有代码都可以缩短:
$statement_date = date( 'y-m-d', strtotime( $statement_date ) );
$date_found = array_search( $statement_date, $date );
$nextdate = $date[$date_found+1];
echo date( 'm/d/y', strtotime($nextdate) );
我使用了date()
来格式化$statement\u date
,因为我不知道它的格式,但是如果它是y-m-d
,你可以直接搜索它
array\u search()
返回相应的键,然后获得$nextdate
按1递增键
要格式化$nextdate
,您可以根据其他答案使用date()
或DateTime->format()
- 阅读更多关于
- 阅读更多关于
$nextdate=$date[$i+1]
可以访问超出$date
数组上限的元素,前提是循环计数$i
到大小($date)
$statement_date = date( 'y-m-d', strtotime( $statement_date ) );
$date_found = array_search( $statement_date, $date );
$nextdate = $date[$date_found+1];
echo date( 'm/d/y', strtotime($nextdate) );