英文:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论