繁体   English   中英

无法在php中添加到当前时间

[英]Unable to add to current time in php

我无法将随机的小时,小时,分钟和秒数添加到当前日期。

这是我的代码-

$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);    

$newTime = date("d/m/Y H:m:s",strtotime(" +'.$randnos.' days" . " +'.$randhrs.' Hours". " +'.$randmin.' minutes". " +'.$randsec.' seconds"));

update_post_meta($post_id,'test',$newTime);

您的引号都弄糟了。

$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);    

$newTime = date("d/m/Y H:m:s",strtotime("+$randnos days +$randhrs hours +$randmin minutes +$randsec seconds"));

update_post_meta($post_id,'test',$newTime);

一衬垫

echo date('Y-m-d h:i:s', strtotime('+' .rand(30, 60 * 60 * 24 * 3).' seconds'));

strtotime()使用的string问题。

$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);    
$str = ' +'.$randnos.' days +'.$randhrs.' Hours  +'.$randmin.' minutes +'.$randsec.' seconds';
$newTime = date("d/m/Y H:m:s",strtotime($str));

首先使用随机数据创建字符串,然后插入strtotime()

$randnos = rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);    
$sec =  $randsec + ($randmin * 60) + ($randhrs * 60 * 60 ) + ($randnos * 60 * 60 * 24);
$newTime = date("d/m/Y H:m:s",strtotime(date("r")) + $sec );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM