I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.
Related
Been trying to figure out just how to add hours, days, weeks and years to a date. Found a few examples that work, but I have NO idea why.
$dt->add(new DateInterval('P1Y')); 'P1M', 'P1D' all add one year, month and day. 'P1H' or 'P1S' throw exceptions.
Been reading all about DateTime class and reading the https://www.php.net/manual/en/dateinterval.format.php page, NO WHERE can I find anything that explains what the 'P' part of that format string is.
Where is some decent documentation on this??? It should not take hours to figure out how to add a few days to a date!!
The P stands for period. If you want to define an interval based on hours or minutes check this example:
$interval = new DateInterval('PT1H');
Here, $interval represents a time interval of 1 hour.
For example, to add an hour to an existing date:
$date = new DateTime();
$date->add($interval);
This may be a complete noob question but here goes:
I have the following code that compares two dates for absence management. Where I expect the answer to return as 2 (the difference between start and end date) I get 1.
$start_time = new DateTime("2015-01-01 00:00:00");
$end_time = new DateTime("2015-01-02 00:00:00");
$diff = $end_time->diff($start_time);
$d = $diff->days; // 1
I have also tried using just the dates (but I need the times as some absence type are done by hours not days)
Difference is 1 because there is only one day difference between both days.
To convert the datetime into hours or minutes you should look to these links:
Convert datetime into year, month, days, hours, minutes, seconds
Difference between 2 time() values
I run this script which works sweet estimating how much time before something weather related happens. But a midnight it goes crazy and for the whole midnight hour, it returns crazy negative times like -1100 minutes and stuff, then when it gets to 0100 hrs it's back to normal and reports, like 20 mintues etc.
Script:
$timenow = date("H:i:s");
$eventtime= strtotime("$gettimefromtextfile"); //time the weather event will happen in the near future
$TimeEnd = strtotime($timenow);
$Difference = ($eventtime - $TimeEnd);
if ($Difference >= 0) {
$minutes = floor(($Difference / 60));
// print how many minutes until an event happens, discard it if event is in the past
I know the date function had issues with midnight up to PHP 5.3. But I am running PHP 5.3 so shouldn't be an issue. I don't need the date, it is only time I need, weather related stuff is reported only hours difference at most.
Any suggestions on an alternative function or coding that will stop this spasm at midnight?
What about using DateTime::diff? Don't reinvent the wheel!
<?php
date_default_timezone_set('Europe/Lisbon');
$next = new DateTime('18:00:01');
$now = new DateTime();
$diff = $next->diff($now);
echo $diff->format('%h hours, %i minutes');
?>
Reference: http://php.net/manual/en/datetime.diff.php
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have 2 unix time, one is todays date and other is expiration time.
Todays Time: 1377173245 (2013-08-22 12:07:25)
Expiration Time: 1406303166 (2014-07-25 15:46:06)
What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?
Thank you :-)
This is what you're looking for: DateTime::diff
First create object with 1st date:
$date = new DateTime('2013-08-22 12:07:25');
Then use the diff method:
$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));
Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.
Use DateTime::diff
See the doc on https://www.php.net/manual/en/datetime.diff.php
Try using datetime::diff. Here's the example from the php.net documentation:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.
Use DateTime class:
Example:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');
Outputs:
11 months, 21 hours, 44 minutes, and 20 seconds remaining
It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.
$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;
If you need months, you'll find it easier to use date_diff().
I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates?
Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
You can change these dates to a unix timestamp with strtotime.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html