Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP日期函数提供一个随机数_Php - Fatal编程技术网

PHP日期函数提供一个随机数

PHP日期函数提供一个随机数,php,Php,您好,我正在制作一个php脚本,以便在客户生日时发送电子邮件,基本上我会循环查看每个客户的生日,并检查今天的日期,如果它们匹配,则会发送一封电子邮件。虽然date函数给出的是一个随机数,而不是今天的日期,但数字是:1505451600 也许我在代码里做错了什么?有人知道解决这个问题的方法吗 $get_birthday = $DB_con->prepare("SELECT email, dt_nascimento FROM clientes"); if ($get_birthday->

您好,我正在制作一个php脚本,以便在客户生日时发送电子邮件,基本上我会循环查看每个客户的生日,并检查今天的日期,如果它们匹配,则会发送一封电子邮件。虽然date函数给出的是一个随机数,而不是今天的日期,但数字是:1505451600

也许我在代码里做错了什么?有人知道解决这个问题的方法吗

$get_birthday = $DB_con->prepare("SELECT email, dt_nascimento FROM clientes");
if ($get_birthday->execute()) {
    while ($array_birthday = $get_birthday->fetch(PDO::FETCH_ASSOC)) {
        $birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
        // echo "</br> data:".$array_birthday['dt_nascimento'];
        // echo "</br>".$birthdate;
        $now = date("m/d");
        $now = strtotime($now);
        // echo "</br>now: ".$now;
        $email = $array_birthday['email'];
        if ($now == $birthdate) {
            include"PHPMailer/email_birthday.php";
        }
    }
}
$get_birth=$DB_con->prepare(“从客户那里选择电子邮件,dt_nascimento”);
如果($get_birth->execute()){
而($array_birth=$get_birth->fetch(PDO::fetch_ASSOC)){
$birthdate=date('md',strotime($array_birthday['dt_nascimento']);
//echo“
数据:“.$array_birth['dt_nascimento'”; //回声“
”$生日; $now=日期(“m/d”); $now=实时时间($now); //echo“
now:”.$now; $email=$array_birth['email']; 如果($now==$birthdate){ 包括“PHPMailer/email_birth.php”; } } }
它给你一个数字的原因是计算机如何测量时间是自1970年1月1日00:00(UTC(世界时))以来的秒数。

它给你一个数字的原因是计算机如何测量时间是自1970年1月1日00:00(UTC(世界时))以来的秒数.

我删除了$now到时间戳的转换,并将$birthdate格式更改为与$now相同的格式。 这是工作代码:

$get_birthday = $DB_con->prepare("SELECT email, dt_nascimento FROM clientes");
if ($get_birthday->execute()) {
    while ($array_birthday = $get_birthday->fetch(PDO::FETCH_ASSOC)) {
        $birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
        // echo "</br> data:".$array_birthday['dt_nascimento'];
        // echo "</br>".$birthdate;
        $now = date("m d");
        // echo "</br>now: ".$now;
        $email = $array_birthday['email'];
        if ($now == $birthdate) {
            include"PHPMailer/email_birthday.php";
        }
    }
}
$get_birth=$DB_con->prepare(“从客户那里选择电子邮件,dt_nascimento”);
如果($get_birth->execute()){
而($array_birth=$get_birth->fetch(PDO::fetch_ASSOC)){
$birthdate=date('md',strotime($array_birthday['dt_nascimento']);
//echo“
数据:“.$array_birth['dt_nascimento'”; //回声“
”$生日; $now=日期(“MD”); //echo“
now:”.$now; $email=$array_birth['email']; 如果($now==$birthdate){ 包括“PHPMailer/email_birth.php”; } } }
我删除了$now到时间戳的转换,并将$birthdate格式更改为与$now相同的格式。 这是工作代码:

$get_birthday = $DB_con->prepare("SELECT email, dt_nascimento FROM clientes");
if ($get_birthday->execute()) {
    while ($array_birthday = $get_birthday->fetch(PDO::FETCH_ASSOC)) {
        $birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
        // echo "</br> data:".$array_birthday['dt_nascimento'];
        // echo "</br>".$birthdate;
        $now = date("m d");
        // echo "</br>now: ".$now;
        $email = $array_birthday['email'];
        if ($now == $birthdate) {
            include"PHPMailer/email_birthday.php";
        }
    }
}
$get_birth=$DB_con->prepare(“从客户那里选择电子邮件,dt_nascimento”);
如果($get_birth->execute()){
而($array_birth=$get_birth->fetch(PDO::fetch_ASSOC)){
$birthdate=date('md',strotime($array_birthday['dt_nascimento']);
//echo“
数据:“.$array_birth['dt_nascimento'”; //回声“
”$生日; $now=日期(“MD”); //echo“
now:”.$now; $email=$array_birth['email']; 如果($now==$birthdate){ 包括“PHPMailer/email_birth.php”; } } }
1505451600
相当于:
09/15/2017@5:00am(UTC)

这是因为:

date(“m/d”)
返回
9/15
(今天月/日)

然后尝试将该字符串转换为unix时间戳,只要字符串中没有年份,则假定为当前年份(2017年)

一旦时间段不存在,则假定时区为午夜(utc上午5点)


这就是为什么最后时间是1505451600

1505451600
相当于:
09/15/2017@5:00am(UTC)

这是因为:

date(“m/d”)
返回
9/15
(今天月/日)

然后尝试将该字符串转换为unix时间戳,只要字符串中没有年份,则假定为当前年份(2017年)

一旦时间段不存在,则假定时区为午夜(utc上午5点)


这就是为什么最终时间为1505451600的原因。要使代码正常工作,您需要进行两项更改:

(1) 删除此行:

$now = strtotime($now);
$birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
原因:你不需要时间戳。你想要一个格式化的日期

(2) 在此行中更改“m d”:

$now = strtotime($now);
$birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
“m/d”是这样的:


原因:您需要以相同的方式设置
$birthdate
$now
的格式,以使比较生效。

要使代码生效,您需要进行两项更改:

(1) 删除此行:

$now = strtotime($now);
$birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
原因:你不需要时间戳。你想要一个格式化的日期

(2) 在此行中更改“m d”:

$now = strtotime($now);
$birthdate = date('m d', strtotime($array_birthday['dt_nascimento']));
“m/d”是这样的:


原因:您需要以相同的方式设置
$birthdate
$now
的格式来进行比较。

1505451600相当于:09/15/2017@5:00am(UTC),那么为什么它只给出该数字而不是日期和月份?
strotime
为您提供已通过日期的时间戳。你期望什么?为什么不做一个SQL查询,为你提供当天出生的客户机,并删除所有PHP逻辑?1505451600相当于:09/15/2017@5:00am(UTC),那么为什么它只提供该数字而不是日期和月份?
strotime
为你提供一个通过日期的时间戳。你期望什么?为什么不做一个SQL查询,为你提供当天出生的客户端,并删除所有PHP逻辑?虽然你的修复是正确的,但将
“m/d”
-格式与
'md'
-格式进行比较永远不会是正确的。尽管你的修复是正确的,但比较
“m/d”
-格式为
'md'
-格式永远不会是真的。