Basically i have a couple dates that are changed when the next/prev page button is clicked, but i need to cap it so it can only be clicked +/- 7 Days then the button disappears.
here is my controller
$data['date'] = isset($_GET['date']) ? new DateTime($_GET['date']) : new DateTime();
$data['tomorrow'] = new DateTime($data['date']->format('Y-m-d 00:00:00'));
$data['tomorrow']->add(new DateInterval('P1D'));
$data['yesterday'] = new DateTime($data['date']->format('Y-m-d 00:00:00'));
$data['yesterday']->sub(new DateInterval('P1D'));
$data['today'] = date("Y-m-d 00:00:00");
$data['next'] = TRUE;
$data['prev'] = TRUE ;
So if ( date diff of the two dates is more than +/- 7 days ) "die" and buttons dissapear using above variables, we kill page aswell so people cant alter url to access the dates.
If (date diff less than or equal to +/- 6 days) Then show the button
i've tried date_diff but it didnt seem to work , I just need a hand please bare in mind i am an apprentice and have only been using php for 2 months.
You can use the diff method which returns a DateInterval:
<?php
$d1 = new DateTime("2014-03-20");
$d2 = new DateTime();
$dateInterval = $d1->diff($d2);
$nDayBetween = $dateInterval->days;
var_dump($dateInterval);
echo "<br /><br />" . $nDayBetween;
Related
I am trying to create an application in PHP that have a subscription period of 14 days. In my db table, I have start date and expiry date. Iam a bit confused to show up the expiration banner that says "Your application will expire in --- days" .
$start = explode(' ', $billing->started_at); // to get the date only
$end = explode(' ', $billing->expires_at);
$date1=date_create($start[0]);
$date2=date_create($end[0]);
$diff=date_diff($date1,$date2);
Session::put('days_left', $diff->format("%R%a days"));
Please help
In theory, your code should work but you didn't tell us what the actual issue is so here's how I'd approach it.
If you only want to calculate using the date portion of your timestamps, you can just format it before creating your DateTime objects.
<?php
$start = new DateTime(date('Y-m-d', strtotime($billing->started_at)));
$end = new DateTime(date('Y-m-d', strtotime($billing->expires_at)));
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Alternatively, you can simply use the timestamp directly. Depends on what you are trying to achieve.
<?php
$start = new DateTime($billing->started_at);
$end = new DateTime($billing->expires_at);
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Working example:
http://sandbox.onlinephpfunctions.com/code/417d4691483f53fe083735d257df4fb49b832c58
How do I remove time from a date in PHP, for example:
20170803173418 I want to take 4 minutes and 13 seconds away and get the new datestamp that would be 20170803173005
What code do I use to get this?
EDIT
I currently have:
$dbTime = $row['aptDeadline']; // This is the appointment end time stored in DB
$dbTime = new DateTime($dbTime);
$currentTime = date("YmdHis"); // This is the current time
$currentTime = new DateTime($currentTime);
$counterTime = $row['aptDeadline']; //This is the time a countdown clock works from inDB
$counterTime = new DateTime($counterTime);
$difference = $currentTime->diff(new DateTime($dbTime)); // Calculate the time between now and the apt time in the db
I now need some code that if the $difference is positive, can take this figure away from the $counterTime stamp
You can use the modify method of the DateTime class in PHP:
<?php
$time = new \DateTime('20170803173418');
$time->modify('-4 minutes')->modify('-13 seconds');
echo $time->format('YmdHis');
This will print the result you want.
I am trying to retrieve the number of days for a PHP interval. When I run the following piece of code on http://sandbox.onlinephpfunctions.com/:
$duration = new \DateInterval('P1Y');
echo $duration->format('%a');
echo "Done";
I get:
(unknown)Done
What am I doing wrong?
The '%a' will return the number of days only when you take a time difference otherwise it will return unknown.
You can use '%d' to get the days but it will also return 0 in the case of new \DateInterval('P1Y') as it does not convert years to days.
One easy way to get the number of days is to create a DateTime at zero time, add the interval to it, and then get the resulting timestamp:
<?php
$duration = new \DateInterval('P1Y');
$intervalInSeconds = (new DateTime())->setTimeStamp(0)->add($duration)->getTimeStamp();
$intervalInDays = $intervalInSeconds/86400;
echo $intervalInDays;
echo " Done";
The problem is here:
$duration->format('%a');
As the manual says, "Total number of days as a result of a DateTime::diff() or (unknown) otherwise".
You need a valid dateInterval object returned by DateTime's diff() method to make the "a" parameter work with DateInterval::format() function:
$now = new DateTime(date('Y-m-d H:i:s'));
$duration = (new DateTime("+1 year"))->diff($now);
echo $duration->format('%a');
Looks like if the DateInterval object is not created by DateTime::diff(), it won't work.
Hope it helps.
You have to create the interval with real dates:
<?php
$interval = date_diff(new DateTime, new DateTime('+1 year'));
echo $interval->format('%a'), PHP_EOL; // 365
if you want something aware of the year or month context, use this, february will return 28 days, leap years will have their additional day
function interval2days($day, $interval) {
$date = clone $day;
$start = $date->getTimeStamp();
$end = $date->add($interval)->getTimeStamp();
return ($end-$start)/86400;
}
I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.
I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you