I am using the following code to calculate the days remaining to make edits. They have 30 days to make edits, and days count down, this code works perfectly.
<?php
// Calculate days remains to edit or change details
$today = time();
$cdate = strtotime($row_details['payment_date']);//strtotime("19:19:09 Sep 27, 2011");
$dateDiff = $today - $cdate;
$fullDays = floor($dateDiff/(60*60*24));
$dayscalculate = 30 - $fullDays; // Set number of days
echo $dayscalculate.(($dayscalculate == 1) ? " day" : " days");
//
?>
QUESTION: if days = say 3 will say 3 days.. but if days = 0 (is the last of the 30 days).. Then want to say this is your last day or something.. So need an if based on $dayscalculate..
Ideas?
Thank you
How about...
if ($dayscalculate == 0) {
echo 'This is your last day';
} else {
printf('%d day%s',
$dayscalculate,
$dayscalculate > 1 ? 's' : ''
);
}
You may also want to introduce an "out of time" check, ie $dayscalculate < 0 but then again, you may already be handling this scenario.
Related
How do I get the amount of worked minutes from a crossed time range?
For example:
$begindate = "00:00";
$enddate = "08:00";
if(isSaturday($begindate)){
//00:00 - 06:00 || 149%
if(strtotime($begintime) >= strtotime("00:00") and strtotime($begintime) <= strtotime("06:00")){
$weekend149 = true;
}
}
The code currently sees that the person has worked between the hours but I want to get the specific amount he has worked BETWEEN that time range only.
So if he worked from 03:00 till 08:00 I want to have a separate variable that says 3 hours or 180 minutes.
You have two time intervals and are looking for the time span of the overlap. This can be done well with DateTime. Date and time should be used for correct calculations. If only a time is specified, the calculation is made for the current date.
$startdate1 = "03:00";
$enddate1 = "08:00";
$startdate2 = "00:00";
$enddate2 = "06:00";
$startdate1 = date_create($startdate1);
$enddate1 = date_create($enddate1);
$startdate2 = date_create($startdate2);
$enddate2 = date_create($enddate2);
$startOverlap = max($startdate1,$startdate2);
$endOverlap = min($enddate1,$enddate2);
$diff = $startOverlap < $endOverlap
? $startOverlap->diff($endOverlap)
: false
;
echo $diff ? $diff->format('%h:%i:%s') : 'no overlap';
The output is limited to 24 hours. If the difference can be more than 24 hours, you have to include the days in the format.
You can test different times yourself at https://3v4l.org/Fa1sE.
I am trying to get stripe to set a end_trial date on the next occurrence of whatever day of the month the user chooses. i.e. If today is the 16th and the user chooses the 15th I need the unix timestamp for the 15th of the next month. However if today was the 14th I need the timestamp for tomorrow.
I tried the solution found on this SO question Find the date for next 15th using php .
When i ran the code suggested in that question and substituted 15 for 31
$nextnth = mktime(0, 0, 0, date('n') + (date('j') >= 31), 31);
echo date('Y-m-d', $nextnth);
The result is 2013-03-03
I also tried this one Get the date of the next occurrence of the 18th .
The second one would actually give me 2013-03-31 when i ran it one 2013-1-31.
Both had unexpected results. Is february the problem? Any guidance will be much appreciated.
Here is a way to do it.
function nextDate($userDay){
$today = date('d'); // today
$target = date('Y-m-'.$userDay); // target day
if($today <= $userDay){
$return = strtotime($target);
}
else{
$thisMonth = date('m') + 1;
$thisYear = date('Y');
if($userDay >= 28 && $thisMonth == 2){
$userDay = 28;
}
while(!checkdate($thisMonth,$userDay,$thisYear)){
$thisMonth++;
if($thisMonth == 13){
$thisMonth = 1;
$thisYear++;
}
}
$return = strtotime($thisYear.'-'.$thisMonth.'-'.$userDay);
}
return $return;
}
// usage
echo date('Y-m-d',nextDate(29));
We get the user's choice and compare it today.
If today is less than or equal to user choice, we return the timestamp for this month.
If today is greater than user choice, we loop through dates, adding a month (or a year if it's $thisMonth hits 13). Once this date does exist again, we have our answer.
We check the dates using php's checkdate function, strtotime and date.
I really don't understand the question completely. You can easily determine the date for next 30 days for example
$next_ts = time() + 30 * 86400; // add 30 days to current timestamp
$next = date('Y-m-d', $next_ts); // format string as Y-m-d
echo $next;
If that is not what you need, please explain the problem.
I'm trying many approaches but then I get stuck half way.
Let's say order was created today. I need to display when the next recurring order will happen. So I have order created June 13, 2012. Then I have set the schedule to bimonthly recurring order, every 1st of month. How to calculate when the next recurring order will happen? The answer is August 1st.
If someone can outline an approach it would be very useful, it doesn't have to be code. This is what I have so far...
// first, get starting date
$start_date_month = date('m', strtotime($start_date));
// get this year
$this_year = date('Y');
// if this month is december, next month is january
$this_month = date('m', $timestamp_month);
if($this_month == 12){
$next_month = 1;
// if this month is not december add 1 to get next month
}else{
$next_month = $this_month + 1;
}
// get array of months where recurring orders will happen
$months = array();
for ($i=1; $i<=6; $i++) {
$add_month = $start_date_month+(2*$i); // 2, 4, 6, 8, 10, 12
if($add_month == 13){$add_month = 1;$year = $this_year+1;}
elseif($add_month == 14){$add_month = 2;$year = $this_year+1;}
elseif($add_month == 15){$add_month = 3;$year = $this_year+1;}
elseif($add_month == 16){$add_month = 4;$year = $this_year+1;}
elseif($add_month == 17){$add_month = 5;$year = $this_year+1;}
elseif($add_month == 18){$add_month = 6;$year = $this_year+1;}
elseif($add_month == 19){$add_month = 7;$year = $this_year+1;}
elseif($add_month == 20){$add_month = 8;$year = $this_year+1;}
else{$year = $this_year;}
echo $what_day.'-'.$add_month.'-'.$year.'<br />';
$months[] = $add_month;
}
echo '<pre>';
print_r($months);
echo '</pre>';
I don't want to simply find what's the date in two months from now. Let's say order created June 1. Next recurring order is August 1. Then let's say now, today is September 1st, but next recurring order is October 1st. See my dilemma?
Just take the current month, so since it's June, we get 6. 6 mod 2 == 0. Next month is July, we get 7. 7 mod 2 == 1.
So just check if current month % 2 == (first month % 2).
Then just check if it's the 1st of the month.
In PHP modulus is defined with the percentage symbol.
$month = date('n');
$createdMonth = 6;
if($month % 2 == $createdMonth % 2){
// stuff
}
You might find the library called When useful for this (I'm the author).
Here is code which will get you the next 2 recurring monthly dates (from todays date):
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->bymonthday(array(1));
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Taking this a step further, you likely only want to find the 2 first business days:
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->byday(array('MO', 'TU', 'WE', 'TH', 'FR')) // week days only
->bymonthday(array(1, 2, 3)) // the first weekday will fall on one of these days
->bysetpos(array(1)); // only return one per month
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Also note, the code is currently under a rewrite -- it works well but it is a little confusing and not well documented.
strtotime to the rescue:
<?php
date_default_timezone_set('Europe/London');
$d = new DateTime('2012-01-31');
$d->modify('first day of +2 months');
echo $d->format('r'), "\n";
?>
Let's say you want the next six orders:
$order_date = '6/13/2012';
$start = date('Y-m-01', strtotime($order_date));
$order_count = 6;
$future_orders = array();
$next = strtotime('+2 months', strtotime($start));
while(count($future_orders) < $order_count){
$future_orders[] = date('m/d/Y',$next);
$next = strtotime('+2 months', $next);
}
This can, obviously, be improved upon, but it should get you started ...
I got this:
$today = new DateTime();
$target_date = $today->modify("first day of +2 months");
echo "Your event is on " . $target_date->format("d/m/Y") . "!";
I am trying to write a function which checks if a "Finished Lesson" was four days ago. How do I check if said lesson was in that time range, for example. If it was finished yesterday, 2 days ago, 3 days ago, 4 days ago, it would be true since it is in the time range of "4 days ago".
How do I check this?
So far I've done:
$time = time();
$fourDays = 345600;
$threeDays = 259200;
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $lastLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate + $fourDays < $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
Right now, the if statement keeps hitting true which is not what I want since I have a lesson that was finished on the 3rd May. It should be true for a lesson that was finished on the 7th May I guess?
$time = time();
$fourDays = strtotime('-4 days');
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $finishedLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate >= $fourDays && $lastLessonDate <= $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
All calculations should be calculated relative to today at 12am, not time() which gives you the current time now (e.g. 6pm) This is an issue because when you do this, 1 day ago (now - 24hours) means time that is between yesterday 6pm and today 6pm. Instead, yesterday should mean a time between yesterday 12am and today 12am.
Below is a simplified calculation to illustrate the idea:
$lastLessonDate = strtotime($lastLessonDate);
$today = strtotime(date('Y-m-d')); // 12:00am today , you can use strtotime('today') too
$day = 24* 60 * 60;
if($lastLessonDate > $today) // last lesson is more than 12:00am today, meaning today
echo 'today';
else if($lastLessonDate > ($today - (1 * $day))
echo 'yesterday';
else if($lastLessonDate > ($today - (2 * $day))
echo '2 days ago';
else if($lastLessonDate > ($today - (3 * $day))
echo '3 days ago';
else if($lastLessonDate > ($today - (4 * $day))
echo '4 days ago';
else
echo 'more than 4 days ago';
i am develop a webpage in that i need to calculate x days from a specified date , The problem is we exclude the saturday and sunday . For example $Shipdate = '06/30/2009' and the x is 5 means , i want the answer '7' [ie 30 is tuesday so after 5 days it will be sunday , so there is two holiday(saturday and sunday) so we add 5+2(for saturday and sunday)]=7. Please help me to find out , Thanks in advance.
Generally you will need to be able to specify a calendar with significant days excluded. Consider Christmas Day or public holidays. This appears to be code that will consider public holidays, you need to modify it or parameterise it with your set of holidays.
<?php
// ** Set Beginning and Ending Dates, in YYYY-mm-dd format **
$begin = '2008-01-01';
$end = '2008-03-31';
$begin_mk = strtotime("$begin");
$end_mk = strtotime("$end");
// ** Calculate number of Calendar Days between the two dates
$datediff = ( $end_mk > $begin_mk ? ( $end_mk - $begin_mk ) : ( $begin_mk - $end_mk ) );
$days = ( ( $datediff / 3600 ) / 24 );
$days = $days + 1; // to be inclusive of last date;
// ** Count days excluding Sundays **
$iteration = 0;
$numDaysExSunday = 0;
for ($i=1; $i<=$days; $i++) {
$weekday = date("w", strtotime("$begin + $iteration day"));
echo "$weekday<br>";
**// i change only this line to add saturday**
if ($weekday !== '0' && $weekday !== '6') {
$numDaysExSunday++;
}
$iteration++;
}
// ** Output number of days excluding Sundays **
echo $numDaysExSunday;
?>
i take it from
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23499410.html
the solution Excluding Sundays but need to change only one line to exclude saturday i write it in the code
function Weekdays($start,$end){
$days=0;
if ( $end <= $start ) {
// This is invalid data.
// You may consider zero to be valid, up to you.
return; // Or throw an error, whatever.
}
while($start<$end){
$dayofweek = date('w',$start);
if( 6!= $dayofweek && 0 != $dayofweek){
$days++;
}
$start = strtotime('+1 day',$start);
}
return $days;
}
You may want to tweak it a bit depending on whether you want to count it as one day or zero if the start is the same day as the end.