PHP出生日期检查器

PHP出生日期检查器,php,Php,我目前正在使用以下PHP代码检查出生日期,该代码使用美国的mm/dd/yyyy,尽管我正在尝试将其更改为英国的dd/mm/yyyy 我希望有人能告诉我要改变什么: function dateDiff($dformat, $endDate, $beginDate) { $date_parts1=explode($dformat, $beginDate); $date_parts2=explode($dformat, $endDate); $start_date=gregor

我目前正在使用以下PHP代码检查出生日期,该代码使用美国的mm/dd/yyyy,尽管我正在尝试将其更改为英国的dd/mm/yyyy

我希望有人能告诉我要改变什么:

function dateDiff($dformat, $endDate, $beginDate)
{
    $date_parts1=explode($dformat, $beginDate);
    $date_parts2=explode($dformat, $endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;
}

//Enter date of birth below in MM/DD/YYYY
$dob="04/15/1993";
echo round(dateDiff("/", date("m/d/Y", time()), $dob)/365, 0) . " years.";

分解是将字符串分解成一个数组,它使用/作为分隔符

所以对我们来说,你会得到

$date_parts1[0] = 04
$date_parts1[1] = 15
$date_parts1[2] = 1993
您需要交换索引0和1处的值

试试这个:

$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
编辑,添加注释更正: 同时将最后一行更改为

echo round(dateDiff("/", date("d/m/Y", time()), $dob)/365, 0) . " years.";

分解是将字符串分解成一个数组,它使用/作为分隔符

所以对我们来说,你会得到

$date_parts1[0] = 04
$date_parts1[1] = 15
$date_parts1[2] = 1993
您需要交换索引0和1处的值

试试这个:

$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
编辑,添加注释更正: 同时将最后一行更改为

echo round(dateDiff("/", date("d/m/Y", time()), $dob)/365, 0) . " years.";

您能将其转换为使用unix时间戳吗

echo time();
1316631603 (- Mumber of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT))
它将有一个更好的控制

echo date("Y-m-d H:i:s", '1316631603');
2011-09-21 21:00:03

echo date("d-m-Y H:i:s", '1316631603');
21-09-2011 21:00:03
因此,你的问题的答案很简单:

$user_bdate = strtotime("11/30/".date('Y')); // My birthday - Output: 1322607600
$time_to_bdate = $user_bdate-time(); // Take the current date and drag it from my birthday gives x seconds
// Include people who have a week to the birthday and to exclude them after one day
if($time_to_bdate >= -86400 && $time_to_bdate <= 604800) // 86400 seconds in a day - 604800 seconds in a week
{
    echo 'Happy birthday';
}
$user_bdate=strotime(“11/30/”.date('Y');//我的生日-输出:1322607600
$time_to_bdate=$user_bdate-time();//将当前日期从我的生日拖到x秒
//包括离生日还有一周的人,一天后将其排除在外

如果($time_to_bdate>=-86400&&$time_to_bdate可以将其转换为使用unix时间戳吗

echo time();
1316631603 (- Mumber of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT))
它将有一个更好的控制

echo date("Y-m-d H:i:s", '1316631603');
2011-09-21 21:00:03

echo date("d-m-Y H:i:s", '1316631603');
21-09-2011 21:00:03
因此,你的问题的答案很简单:

$user_bdate = strtotime("11/30/".date('Y')); // My birthday - Output: 1322607600
$time_to_bdate = $user_bdate-time(); // Take the current date and drag it from my birthday gives x seconds
// Include people who have a week to the birthday and to exclude them after one day
if($time_to_bdate >= -86400 && $time_to_bdate <= 604800) // 86400 seconds in a day - 604800 seconds in a week
{
    echo 'Happy birthday';
}
$user_bdate=strotime(“11/30/”.date('Y');//我的生日-输出:1322607600
$time_to_bdate=$user_bdate-time();//获取当前日期并将其从我的生日拖到x秒
//包括离生日还有一周的人,一天后将其排除在外

如果($time_to_bdate>=-86400&&$time_to_bdate如果您只需要一个函数以该特定格式返回给定生日的年份,您可以使用类似的方法来避免在函数调用中传递太多数据:

<?php
    function get_age($birthdate){
        list($d, $m, $y) = explode('/', $birthdate);
        $time_birth =  mktime(0, 0, 0, $m, $d, $y);
        return round( (time() - $time_birth) / (60*60*24*365) ) ;
    }

    //example, use dd/mm/yyyy
    $dob="04/15/1993"; 
    echo get_age($dob). " years.";
?>

如果您只需要一个函数以该特定格式返回给定生日的年份,您可以使用类似的方法来避免在函数调用中传递太多数据:

<?php
    function get_age($birthdate){
        list($d, $m, $y) = explode('/', $birthdate);
        $time_birth =  mktime(0, 0, 0, $m, $d, $y);
        return round( (time() - $time_birth) / (60*60*24*365) ) ;
    }

    //example, use dd/mm/yyyy
    $dob="04/15/1993"; 
    echo get_age($dob). " years.";
?>


好吧,你可以查
explode()
gregoriantojd()
自己计算出来……好吧,你可以查
explode()
gregoriantojd()
自己计算出来……你还需要更改
日期(“d/m/Y”,time())
同时,我现在正在使用此方法,但我得到以下错误:警告:gregoriantojd()期望参数1为长字符串…您还需要更改
日期(“d/m/Y”,time())
同时,我现在正在使用此方法,但我得到以下错误:警告:gregoriantojd()预期Parameter 1为长字符串…如果在数据库中存储时间戳,则可以将结果限制为具有生日的人,方法与:WHERE bdate=time()-604800相同如果在数据库中存储时间戳,则可以将结果限制为具有生日的人,方法与:WHERE bdate=time()-604800相同