简体   繁体   English

在PHP中将前导零添加到字符串日期

[英]Adding leading zeroes to a string date in PHP

I have a string "date" which can be DD.MM.YYYY or DMYYYY (with or without leading zeros), it depends what a user types. 我有一个字符串“ date”,可以是DD.MM.YYYY或DMYYYY(带或不带前导零),它取决于用户键入的内容。 Then I use it in a condition to send another email when the day is today. 然后,我可以在今天今天有条件的情况下使用它发送另一封电子邮件。

if($_POST["date"]== date("d.m.Y")){ 
  $headers.="Bcc: another@mail.cz\r\n";
}

The problem is that the mail is send when the date format DD.MM.YYYY (with leading zeros) only. 问题是仅当日期格式为DD.MM.YYYY(带前导零)时才发送邮件。

My proposed solution 我建议的解决方案

As I'm not very good in PHP, I only know the solution theoretically but not how to write the code - I would spend a week trying to figure it out on my own. 由于我不太擅长PHP,因此我从理论上只知道解决方案,但不知道如何编写代码-我将花一个星期的时间自行解决。

What's in my mind is dividing the date into three parts (day, month, year), then checking the first two parts if there's just one digit and adding leading zeros if it's the case. 我的想法是将日期分为三部分(天,月,年),然后检查前两个部分是否只有一位数字,如果是,则添加前导零。 I don't know how to implement that to the condition above, though. 但是,我不知道如何实现上述条件。 I have read a few topics about how to do this, but they were a bit more different than my case is. 我已经阅读了一些有关如何执行此操作的主题,但它们与我的情况有所不同。

You should equalize to same format dmY and you can do this with strtotime and date function: 您应该等于相同格式的dmY并且可以使用strtotimedate函数来实现:

$post_date = date("d.m.Y", strtotime($_POST["date"]));

if($post_date == date("d.m.Y")){ 
  $headers.="Bcc: another@mail.cz\r\n";
}

I changed date to $post_date for more clear. 我将date更改为$post_date更清楚。 I'll try to explain difference with outputs 我将尝试解释与输出的差异

echo $_POST["date"]; // lets say: 8.7.2013

echo date("d.m.Y"); // 09.09.2013 > it's current day

strtotime($_POST["date"]); // 1373230800 > it's given date with unix time

$post_date = date("d.m.Y", strtotime($_POST["date"])); // 08.07.2013 > it's given date as right format

If you use date function without param, it returns as current date. 如果使用不带参数的date函数,它将作为当前日期返回。

Otherwise if you use with param like date('dmY', strtotime('given_date')); 否则,如果您使用类似date('dmY', strtotime('given_date')); , it returns as given date . ,它将按given date返回。

$post_date = date("d.m.Y", strtotime($_POST["date"]));

At first, we converted your date string to unix with strtotime then equalized and converted format that you used in if clause. 首先,我们使用strtotime将您的日期字符串转换为unix,然后使用if子句中经过均衡和转换的格式。

first set date format with leading Zero 设置日期格式的前导零

$postdate = strtotime('DD.MM.YY', $_POST['date']);

and also matching date will be in same format 并且匹配日期将采用相同格式

$matching_date = date('DD.MM.YY', strtotime('whatever the date'));

then 然后

if ( $postdate === $matching_date )
{
  // send mail
}

Why don't you just check the length of the _POST (it can be either 8 or 10) 您为什么不只检查_POST的长度(它可以是8或10)

if (strlen($_POST["date"]) == 10) {
   $headers.="Bcc: another@mail.cz\r\n";
}

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

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