PHP日期自动将月份名称转换为数字

时间:2015-07-25 00:26:32

标签: php

我使用以下代码发送包含日期/时间的电子邮件。出于某种原因,这个月将作为一个数字出现,但不会像人们想象的那样1-12,而是107。

我的代码是:

require('/home/jollyrogerpcs/public_html/settings/globalVariables.php');
require('/home/jollyrogerpcs/public_html/settings/mysqli_connect.php');
mysqli_select_db($conn,"newsletterlist");
$query = "SELECT * FROM newsletterusers";
$result = mysqli_query($conn, $query);

$subject = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['subject']);
$message = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['body']);


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Jesse Elser<jesse@example.com>' . "\r\n";

if (!$result) exit("The query did not succeded");
else {
    while ($row = mysqli_fetch_array($result)) {
        $to = $row['email'];
        $encodedTo = rtrim(strtr(base64_encode($to), '+/', '-_'), '=');
        $date = date_default_timezone_set("America/Chicago");
        $date .= date("m/d/Y h:i:sa");
        $date .= " CST";
        $body ='<!DOCTYPE HTML>';
        $body .='<body style="padding: 0; margin: 0; background-color: #000; color: #fff; text-align: center; font-family: verdana;">';
        $body .='<div id="container" style="width: 90%; margin: 0 auto; text-align: left; background-color: #121212;">';
        $body .='<div id="header" style="border-bottom: 1px solid #ff6400;">';
        $body .='<img src="http://example.com/images/main/logo.png" width="100%">';
        $body .='</div>';
        $body .='<div id="subject" style="background-color: #121212; text-align: center;">';
        $body .='<h1 style="color: #ff6400; margin: 0;">'.$subject.'</h1>';
        $body .='</div>';
        $body .='<div id="message" style="background-color: #232323; color: #fff; padding: 10px;">';
        $body .=  $message;
        $body .='</div>';
        $body .='<div id="footer" style="background-color: #121212; padding: 10px;">';
        $body .='<a href="http://example.com" style="text-decoration: none; color: #ff6400;">Visit Our Site</a> | Thanks for subscribing to our newsletter! | <a href="http://example.com/scripts/php/unsubscribe.php?id='.$encodedTo.'" style="text-decoration: none; color: #ff6400;">Unsubscribe</a> <br> E-mail sent: ';
        $body .= $date;
        $body .='</div>';
        $body .='</body>';
        mail($to,$subject,$body,$headers);
    }
}
mysqli_close($conn);
header('Location: http://example.com/newsletter.php');

在我的一封测试电子邮件中,日期应为107/24/2015 07:20:29pm CST,应该是07/24/2015 07:20:29pm CST

1 个答案:

答案 0 :(得分:3)

这是因为将bool与日期字符串连接起来。只需在这些行中查看您的代码:

$date = date_default_timezone_set("America/Chicago");
$date .= date("m/d/Y h:i:sa");

date_default_timezone_set 函数返回 TRUE FALSE 。在这种情况下,true表示为1。

只需将您的代码更改为:

date_default_timezone_set("America/Chicago");
$date = date("m/d/Y h:i:sa");

它应该可以正常工作。