比较来自API的日期字符串。

huangapple go评论82阅读模式
英文:

Compare date string from API

问题

从API调用中,我得到了以下日期,我假设它是一个字符串格式:[end_date] => 2018-09-20

现在,我在我的PHP脚本中有自己创建的日期:$date = '2017-09-20';

现在,我想检查API请求中的end_date是否大于我自己创建的日期字符串。

我尝试了以下方法:

if ($req['end_date'] > strtotime('2019-12-01') || !$req['end_date'])

然而,它没有起作用。帮助将不胜感激!

我还尝试了:

if (strtotime($req['end_date']) > strtotime('2019-12-01') || !$req['end_date'])

如果API响应中没有end_date,if语句也应该通过。这就是or条件的作用。

英文:

From an API call i get the following date I suppose as a string format : [end_date] => 2018-09-20

Now I have my own created date in my PHP script: $date = '2017-09-20';

Now I want to check if end_date from the API request is larger than my own created date string.

I tried the following :

  if($req['end_date'] > strtotime('2019-12-01') || !$req['end_date'])

It didnt work tho. Help is appreciated!

Also tried :

  if(strtotime($req['end_date']) > strtotime('2019-12-01') || !$req['end_date'])

The if statement should also pass when there is not end_date from the api response. Thats what the or is for.

答案1

得分: 1

Use the DateTime class. https://www.php.net/manual/en/class.datetime

$theirDate = new DateTime($theirDateString);
$yourDate = new DateTime($yourDateString);

if ($theirDate > $yourDate) {
    // voila!
}

I'm sure you know how to check for an actual date string before creating the objects too 比较来自API的日期字符串。

英文:

Use the DateTime class. https://www.php.net/manual/en/class.datetime

$theirDate = new DateTime($theirDateString);
$yourDate = new DateTime($yourDateString);

if ($theirDate > $yourDate) {
    // voila!
}

I'm sure you know how to check for an actual date string before creating the objects too 比较来自API的日期字符串。

huangapple
  • 本文由 发表于 2020年1月7日 00:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定