在PHP中,可以使用以下方法计算两个日期字符串之间的天数差异:

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

How the Number of days can be calculated between two strings in PHP?

问题

`$flyingdate = date('Y-m-d H:i:s');
$task_end_date = '2023-05-15'; 
$delays = ($flyingdate - $task_end_date);`

问题是,字符串变量不能被改变,
我尝试过使用日期差异和字符串比较函数,但都没用。
它无法告诉确切的天数差异,也就是实际上从飞行到任务结束日期的延迟,
我想要以天为单位的差异(存储在 $delays 中),并且条件应该是
如果天数为负数,则将延迟设置为零;

英文:
`$flyingdate = date('Y-m-d H:i:s');
$task_end_date = '2023-05-15'; 
$delays = ($flyingdate - $task_end_date);`

Problem is that, String variables can not no be changed,
I tried with date difference and string comparison function but useless
It can not tell the exact difference of days i.e. actually telling delays of days from flying to task end date,
I want difference in term of days(store in $delays) and also condition should be
delays set to zero , if days are negative;

答案1

得分: 1

嗨,Hafiz,我希望你一切都好,正如我所看到的,当你声明$task_end_date变量时,你将它声明为字符串,而$flyingdate你使用了日期类。我建议你在两个变量中都使用日期类,这样你可以计算两个日期之间的天数差异。以下是我建议你使用的代码:

$flyingdate = new DateTime();
$task_end_date = new DateTime('2023-05-15');

// 计算日期差异
$interval = $flyingdate->diff($task_end_date);

// 将差异格式化为天数
$delays = $interval->format('%r%a');

// 检查差异是否为负数
if ($delays < 0) {
    $delays = 0;
}

// 显示延迟
echo "延迟:$delays 天";

正如你在这段代码中看到的,我们使用了DateTime对象来表示飞行日期和任务结束日期。然后,我们使用diff()方法计算两个日期之间的差异。我们使用%r%a格式来获取有符号的天数差异,然后使用条件来检查日期差异是否为负数。祝您有一个愉快的一天,先生。

英文:

Hi Hafiz i hope you are doing well, as i can see that when you declare $task_end_date variable you declared it as a string and $flyingdate you use date class what i recommend is to use date class in both variables, so you can calculate the difference in days between two dates there is the code i recommend you to use :

$flyingdate = new DateTime();
$task_end_date = new DateTime(&#39;2023-05-15&#39;);

// To calculate the difference between the dates
$interval = $flyingdate-&gt;diff($task_end_date);

// Change the difference to days format
$delays = $interval-&gt;format(&#39;%r%a&#39;);

// Check if the difference is negative
if ($delays &lt; 0) {
    $delays = 0;
}

// Display the delays
echo &quot;Delays: $delays days&quot;;

As you can see in this code,we have DateTime objects for the flying date and the task end date. Then, we calculate the difference between the two dates using the diff() method. We use the %r%a format to get the signed difference in days, then we use a condition that check if the date difference in negative.
have a good day sir .

答案2

得分: 0

你可以这样做,time()返回当前时间戳,strtotime也返回时间戳,所以减去两者并将秒转换为天

$flyingdate = time();
$task_end_date = strtotime('2023-05-15');
$delays = $flyingdate - $task_end_date;

$days_diff = $delays / (60 * 60 * 24);
$rounded_days_diff = (int)floor($days_diff);
英文:

You can do like this, time() returns the current timestamp, strtotime also returns timestamp, so substracting both and converting the seconds to days

$flyingdate = time();
$task_end_date = strtotime(&#39;2023-05-15&#39;);
$delays = $flyingdate - $task_end_date;

$days_diff = $delays / (60 * 60 * 24);
$rounded_days_diff = (int)floor($days_diff);

答案3

得分: -1

It worked, thanks
but now, the problem is that I am working in PHP with a database on localhost, each time my $flyingdate and $delays are updated,
I want it to update only one time. I want this if condition to run once when $status == 100 and some condition that show do not enter in loop, I tried with empty $flyingdate, but it become empty every time I run the code, I want some condition that it will always work one time, as in below code,

  if(($status == 100) && (empty($flyingdate))){
        $flyingdate = date('Y-m-d H:i:s');
        $flyingtime = time();
        $task_end_date = strtotime($last_end_date);
        $delays1 = $flyingtime - $task_end_date;
        $days_diff = $delays1 / (60 * 60 * 24);
        $delays = (int)floor($days_diff);
    }
英文:

It worked, thanks
but now, the problem is that I am working in PHP with a database on localhost, each time my $flyingdate and $delays are updated,
I want it to update only one time. I want this if condition to run once when $status == 100 and some condition that show do not enter in loop, I tried with empty $flyingdate, but it become empty every time I run the code, I want some condition that it will always work one time, as in below code,

  if(($status == 100) &amp;&amp; (empty($flyingdate))){
        $flyingdate = date(&#39;Y-m-d H:i:s&#39;);
        $flyingtime = time();
        $task_end_date = strtotime($last_end_date);
        $delays1 = $flyingtime - $task_end_date;
        $days_diff = $delays1 / (60 * 60 * 24);
        $delays = (int)floor($days_diff);
    }

huangapple
  • 本文由 发表于 2023年6月22日 04:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526856.html
匿名

发表评论

匿名网友

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

确定