i want to show date to user from how much days website is running
e.g today : runing from 10 days . next day : running from 11 days
$current = time();
$initial_date = strtotime("2017-11-1"); //You will have to fix this
$datediff = $current - $initial_date;
$num_of_days = floor($datediff / (60 * 60 * 24));
echo "Running from ".$num_of_days." days";
Note: This will not count today.
It's not laravel specific, you can use this in PHP generally:
exec("uptime", $uptimeVar);
echo($uptimeVar[0]);
You can also format it any way you like.
This way helpful to you. first define the web site starting date as a constant as follow:
define('START_DATE', '2017-11-02');
Then you have to put below code in to the place you have to show the date difference:
$date1 = new DateTime(START_DATE);
$date2 = new DateTime(date('Y-m-d'));
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
echo $diff->days . " day(s)"; // output '6 day(s)'
Output looks like:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 6
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 6
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
6 day(s)
Related
I am trying to calculate the difference between two dates to get difference in days, months and years but as a result I get 0 on all parameters. I am relatively new to this, for some I am making an obvious mistake, but I cannot understand what I am doing wrong. Would anyone be kind enough to clarify this? I appreciate any answers / help, thanks.
Update
After Markus Zeller's answer I modified my code as follows and it worked. I will make further changes to better suit my needs, but the code as it is works fine. It does not provide the total number of days and months, but these are displayed as age (example 1 year, 3 months and 14 days) and not as (example 1 year 15 months and 471 days). In these two examples the dates were start: 04/15/2021 - end: 07/30/2022
Only problem left
Following the examples above, the correct past days would be 15 (or 471), while I get 14 (or 470). Why is there this 1 day error ?
function difference_between_date() {
$data1 = new DateTime(wp_get_current_user()->user_registered); //Registered User Date
$data2 = new DateTime(); //Current Date
$interval = $data1->diff($data2);
$diffInSeconds = $interval->s; //example 45
$diffInMinutes = $interval->i; //example 15
$diffInHours = $interval->h; //example 6
$diffInDays = $interval->d; //example 20
$diffInMonths = $interval->m; //example 4
$diffInYears = $interval->y; //example 2
echo '<div class="today_date">days' . wp_kses_post($diffInDays) . '</div>';
echo '<div class="today_date">months' . wp_kses_post($diffInMonths) . '</div>';
echo '<div class="today_date">years' . wp_kses_post($diffInYears) . '</div>';
} add_shortcode('diff_date', 'difference_between_date');
PHP has very good DateTime functions. Use them for example:
$date1 = new DateTime('2022-07-30 15:00:00');
$date2 = new DateTime('2020-01-29 14:30:00');
$diff = $date2->diff($date1);
print_r($diff);
gives you all the properties you need. In this example 2 years, 6 months, 1 day and 30 mins or 913 days in total.
DateInterval Object
(
[y] => 2
[m] => 6
[d] => 1
[h] => 0
[i] => 30
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 913
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
I would like to return the hours and minutes of difference between two dates. The problem is that the result of the diff method returns me a diff of 0 (same DateTimes).
Here is my actual code :
$last_vote_date = new DateTime(\Auth::user()->last_vote_at);
$next_vote_date = $last_vote_date;
$next_vote_date->add(new DateInterval('PT3H'));
$diff = $next_vote_date->diff($last_vote_date, true);
$vote_hours = $diff->format('%h h %I m');
I debugged my vars. $last_vote_date is the correct DateTime.
$next_vote_date is a correct DateTime representing the last vote date + 3 hours.
However all the properties of the DateInterval object that diff returns are all 0 :
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
I really don't understand what's going wrong as my two DateTime objects being compared are different and are exactly the value I want.
When you do $next_vote_date = $last_vote_date you are basically copying a reference. Meaning any changes you make to $next_vote_date will affect $last_vote_date. The quick way to deal with this is to copy the object:
$last_vote_date = new DateTime(\Auth::user()->last_vote_at);
$next_vote_date = clone $last_vote_date;
$next_vote_date->add(new DateInterval('PT3H'));
$diff = $next_vote_date->diff($last_vote_date, true);
$vote_hours = $diff->format('%h h %I m');
I have set times in SQL in this format: 2016-01-03 12:13:26.
I would like to calculate the number of hours and minutes (if hours<1) going from NOW() to that particular SQL time.
I've been looking at all the different threads here but I can't seem to grasp how to convert PHP different time formats to SQL's.
This is the code I've been using, but this will only give me back hours up to 12, and minutes also. Don't know how to use it with days.
$now = date("d/m/Y h:i:s");
$commentime = strtotime($SQLTIME);
$timetocomment = $now - $commentime;
For instance, this code will yield "12 hours ago" for data I posted 24 hours ago to SQL.
How can I do it? Thank you.
This is my suggestion to use date() in this format date("Y-m-d h:i:s"). Than you will get the complete difference in an array.
function dateDifference($date_1 ,$date_2)
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval;
}
$now = date("Y-m-d h:i:s");
$sqlTime = "2016-01-03 12:13:26";
$DateDiffArr = dateDifference($now,$sqlTime);
echo "<pre>";
print_r($DateDiffArr);
Result Is:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 22
[i] => 45
[s] => 55
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 0
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
In resultant array, you can get the all difference as you need like in years, months, days, minutes, seconds etc.
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
Im looking for the less complicated way to do the following:
$joinDate = "2014-05-26"
$date = date('Ymd'); //todays date
$memberFor = $joinDate - $date //This is where I need to get total number of days
Is there a function that can help me with this?
You should be using DateTime Object for these operations
$joinDate = "2014-05-26";
$joinDate_obj = new DateTime($joinDate);
$now = new DateTime();
$interval = $joinDate_obj->diff($now);
$diff = $interval->d ;
echo $diff; //12
The object $interval will have
DateInterval Object (
[y] => 0
[m] => 11
[d] => 12
[h] => 12
[i] => 49
[s] => 4
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 347
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0 [have_special_relative] => 0
)
So you may use for example $interval->days for the difference in days.
$start = new DateTime('2014-01-07', new DateTimeZone('UTC'));
$end = clone $start;
$start->sub(new DateInterval('P1M')); // substract one month
echo $start->format('Y-m-d').' - '.$end->format('Y-m-d');
// 2013-12-07 - 2014-01-07 - seems correct, let's get number of days...
print_r($end->diff($start));
// DateInterval Object ( [y] => 0 [m] => 2 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [invert] => 1 [days] => 61 )
61 days??? What is going on in here?
try this,
$diff=date_diff($date1,$date2);