2015-11-03 113 views
0

我使用cron与PHP脚本导入内容,并且有一个名为_expiration_date的字段,我需要根据当前导入日期动态填充20天。如果我使用PHP程序手动创建记录并查看记录,则该程序将以下列格式在_expiration_date字段中创建数据:1449203160,其转换为2015年12月4日@ 4:26中的人形。用PHP插入动态DATE元数据

我认为这是某种日期/时间戳?

正如你所看到的,我填充的字段是静态文本。如何以正确的日期/时间格式动态填充_expiration_date字段以供系统读取?

 } 
     update_post_meta($post_id, 'adverts_location', $parent->name); 
     update_post_meta($post_id, 'adverts_person', 'John Doe'); 
     update_post_meta($post_id, 'adverts_email', '[email protected]'); 
     update_post_meta($post_id, '_expiration_date', ''); 
    } 

回答

1

这是一个Linux时间戳。 要填充的到期日,你可以使用:

strtotime('+20 days'); 
1
$convertedDate = DateTime::setTimestamp($post_id); 

这会将时间戳转换为日期时间,现在你可以操纵它作为一个正常的日期时间

$expirationDate = date_add($convertedDate , date_interval_create_from_date_string('20 days')); 

或者,如果你可以试试这个你有PHP 5或更高版本

$convertedDate = date('Y-m-d','1449203160'); 
$convertedDate = new DateTime($convertedDate); 
$convertedDate->add(new DateInterval('P20D')); 
echo($convertedDate); 
+0

update_post_meta()的值是什么?这个? @lucarnosky update_post_meta($ post_id,'_expiration_date','$ expirationDate'); –

+0

是的,在expirationData你有datetime的日期时间 – Lucarnosky

+0

这不起作用,它插入$ expirationDate作为数据到字段@lucarnosky –