获取php中两个日期之间的天数 [英] get number of days between two dates in php

查看:77
本文介绍了获取php中两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取两个日期之间的天数,但返回的结果不正确.这是我的尝试;

I'm attempting to get the number of days between two dates but the results being returned are incorrect. Here is my attempt;

  $t_time = get_the_time( __( 'Y/m/d g:i:s A', 'woocommerce' ), $post ); //date of post
    $start_date = new DateTime(); //current date
    $since_start = $start_date->diff(new DateTime($t_time)); //difference
    echo $since_start->d; //number of days

我哪里出错了?它适用于前几个帖子,但当月份发生变化时它会重新启动.

Where did i go wrong? It works for the first couple of posts but then it restarts when the month changes.

推荐答案

试试这个

<?php

     $now = time(); // Current time 
     $your_date = strtotime("2013-12-01"); // This will parses an English textual datetime into a Unix timestamp
     $datediff = abs($now - $your_date);// Gives absolute Value 
     echo floor($datediff/(60*60*24)); //Returns the lowest value by rounding down value 

?>

这篇关于获取php中两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆