Hy,我想数数演员的年龄。
演员的出生日期存储在自定义字段中。
$date = get_field(\'data_nasterii\');
自定义字段中存储的日期为:yymmdd
我怎样才能把bouth粘在一起让它工作呢。
<?php
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = "12/17/1967";
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
echo $age ."ani";
?>
我尝试过这个,但这让我在2012年得到了回报
<?php
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = get_field(\'data_nasterii\');
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
echo $age ."ani";
?>
最合适的回答,由SO网友:kaiser 整理而成
假设你的约会方式——无论出于什么原因——真的是yymmdd
而不是设置为您在“管理设置”部分中定义的内容:
还有本机PHP函数date_parse_from_format()
可以与core设置一起使用。只需添加以下插件并在模板中使用自定义模板标记。
<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: Real Date Template Tag
*/
function wpse85476_to_real_date( $date, $format )
{
static $date_format;
empty( $date_format ) AND $date_format = get_option( \'date_format\' );
$date_parts = date_parse_from_format(
$format
,$date
);
// Error handling
if ( 0 < $date_parts[\'error_count\'] )
{
return current_user_can( \'manage_options\' )
? new WP_Error(
\'date\'
,\'Wrong date format. Adjust Date Parser.\'
,$date
)
: $date;
}
return array(
\'date_parts\' => $date_parts
,\'real_date\' => date_i18n(
$date_format
,mktime( 0, 0, 0, $date_parts[\'month\'], $date_parts[\'day\'], $date_parts[\'year\'] )
)
);
}
下面是如何检索格式正确的日期。
$date = wpse85476_to_real_date( get_field( \'data_nasterii\' ), \'yymmdd\' );
$real_date = $date[\'real_date\'];
下面是如何检索零件进行计算:
$date_parts = $date[\'date_parts\'];