繁体   English   中英

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

[英]PHP Date Converting Month Name To Number Automatically

我正在使用以下代码发送包含日期/时间的电子邮件。 出于某种原因,该月份以数字形式出现,但不是107所认为的1到12。

我的代码是:

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 CST107/24/2015 07:20:29pm CST应为107/24/2015 07:20:29pm CST 07/24/2015 07:20:29pm CST

这是因为将布尔值与日期字符串连接在一起。 只需在这些行中查看代码即可:

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

date_default_timezone_set函数返回TRUEFALSE 在这种情况下,true代表一个。

只需将代码更改为:

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

它应该可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM