简体   繁体   English

在PHP中如何将字符串转换为日期?

[英]In PHP how do I convert string to date?

In C# I can do something like: 在C#中,我可以执行以下操作:

string a = "03/12/2012";
            DateTime ab = DateTime.Parse(a);
            string b = DateTime.Now.ToShortDateString();
            DateTime c = Convert.ToDateTime(b);
            if(ab > c)
            {
                Console.WriteLine("tomorrow");
            }
            else
            {
                Console.WriteLine("Yesterday");
            }

            Console.ReadKey();

How can I do something like this in PHP? 如何在PHP中执行类似的操作? I'm currently new to PHP and I'm still studying most of its features and functions. 我目前对PHP还是陌生的,我仍在研究其大多数功能。 Sir/Ma'am your answers would be of great help and be very much appreciated. 先生/女士,您的回答将为您带来很大的帮助,我们将不胜感激。 Thank you++ 谢谢++

$now = new DateTime;
$ab = DateTime::parseFromFormat($ab);

if ($ab > $now) {
  // Some time in the future
} else {
  // Some time in the past
}

And so an. 等等。 For a full documentation, see the manual about date and time related functions and classes 对于一个完整的文档,请参阅手册有关日期和时间相关的函数和类

You could try this, i tink it would work, you convert the string in a timestamp and compare it to the current timestamp: 您可以尝试一下,我认为它可以工作,您可以将字符串转换为时间戳并将其与当前时间戳进行比较:

$a = strtotime("03/12/2012");
$b = time();
if (a > b) {
//Actually it could be tomorrow or later today or 10 years in the future...
  echo 'tomorrow';
}
else {
//The same as above... it could be yesterday or any date until 1970 (when UNIX timestamp begins)
  echo 'yesterday';
}

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

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