Getting php to properly add days to date - php

I have a invoicing system that generates the next due date after the invoice is paid. My problem though is I want to generate the next invoice based on the date the last one was due, not when it was paid.
I'm familiar with adding days to the current date such as this:
$nextduedate = date('Y-m-d', strtotime("+30 days"));
Lets say the invoice was due on 2016-05-08 but it was paid on 2016-05-12
How would I get the system to add 30 days to my variable $dueDate which is being pulled from the database and set the next invoices due date 30 days from the prior?

Use DateTime():
$dueDate = new DateTimeImmutable('2016-05-08');
$nextInvoice = $dueDate->modify('+30 days');
echo $nextInvoice->format('Y-m-d');

Try this:
$nextduedate = ('Y-m-d', strtotime($duedate. ' + 30 days'));
That will format your date, then add 30 days to the old due date stored in a variable.

Related

Compare date with next month date in php

I have a query, A user fill records of every day for each month and I want, the user can edit/delete his data till 7th of next month.
I got the diff between dates using date() function, but can't understand how to compare that record fill date is less than 7th of next month.
//$fill_date; get data filled date from database
$filled_date = date("Y-m-d", strtotime($fill_date));
$datestring = $filled_date.' first day of next month';
$dt=date_create($datestring);
$d_mont = $dt->format('Y-m-07'); // give 7th of next month from data insert date
//get next month from fill date
$f_date = date('Y-m-d' , strtotime($fill_date));
if(strtotime($f_date) <= strtotime($d_mont)) {
echo strtotime($f_date)." <= ".strtotime($d_mont);
echo "you can edit";
}
There I share a query which can fetch the data till 7th.May this query help you if found any issue then comment.
this table contains last data till 7th of every month otherwise
no data.
SELECT * from datatable where day(curdate()) <= 7 and cast(datefield as date) >= (curdate() + interval -1 month);
Here is an example code
$curdate = date('Y-m-d', strtotime($fill_date)); // or time() for current date
$month = date('m', strtotime($curdate));
$year = date('Y', strtotime($curdate));
$nextmonth = ($month == 12)? 1: $month+1;
$date2 = date("Y-m-d", mktime(0, 0, 0, $nextmonth, 7, $year));
You can compare the dates with strtotime as you already do
What your asking to do is very confusing, but I understand, because I am cool like that.
The best way to do this to set a flag in the data, have a field in the database that is called locked or such that is a simple boolen value 1=true, 0=false
Then on the 7th of the month at midnight run a cron job that updates all the records before the current month with a 0 and set them to a 1.
It would be pretty trivial to write a cron job to do that maybe 20 lines of code tops.
Brief example (psudo code)
$date = (new DateTime())->modify('first day of this month')->format('Y-m-d');
$Sql = "UPDATE tbl SET locked = 1 WHERE DATE(date_field) < '{$date}' AND locked=0";
$DB->query($Sql);
Cron: if you don't know what it is
https://en.wikipedia.org/wiki/Cron
Use cron to schedule jobs (commands or shell scripts, including PHP )
to run periodically at fixed times, dates, or intervals.
If they can only edit tell the 7th of the next month, then on the 7th of each month all the data from last month is no longer editable.
Then when you pull them to edit, you just do
SELECT * FROM table WHERE locked = 0
That is if you truly what it to stop on the 7th and not a month. AS I said in the comments if I put a record on the 1st that gives me that whole month + 7 days, if I did it on the last day, I would have only 7 days to edit the record.
It's not as trivial to write a date range query for this as it first seems. Because if it's before the 7th, you have to select everything from last month and everything from this month ( tell the current date ). But if it's the 8th, you have to select everything from the beginning of the month tell the current data ( omitting last month ). So the query would change depending on the day it currently is.
To try to filter the data after pulling it out seems like a waste, because you will always pull more records then the user can edit, and then you have to work out the date switch anyway.
An advantage of having a locked field also, it that you can selectively unlock a record for a user so they could edit it again, just by fliping the 1 back to a 0. ( -note- the cron job I outlined above would re-lock it ) The point is it would be possible to allow them to edit specific records without code changes.
IMO, it's the best way to do it.

How to stop reservations before today

I'm trying to create a reservation system for a games library of some sort.
Users should not be allowed to reserve the game for a day before today.
I tried to do this by changing the date chosen by the user for the start of the reservation to a timestamp. Then I would set the date for today, change it to a timestamp and check if the date chosen by the user is less than todays timestamp.
Here is the code:
$timestamp = strtotime($ReservationStart);
$todaystamp = (strtotime('yesterday midnight'));
if ($timestamp < $todaystamp) {
die("The date you've chosen is before today, please choose a valid date");
}
I thought this would work but it this code only stops reservations for 2 days past and behind rather than yesterday and behind.
Any ideas on how to get it to work for yesterday?
What you are looking for is to use midnight rather than yesterday midnight.
$timestamp = strtotime('midnight');
It seems that the strtotime works backwards, so yesterday midnight would be yesterday at 00:00, so it allows all 24 hours of the next day (yesterday) to be allowed to book in.
midnight would go to the current day's midnight (starting at that current date's 00:00.
If you were worried about getting today's midnight, that would be:
$timestamp = strtotime('today midnight');
So it's quite easy to understand once you learn that.

How to check in php if today is a 'due date'

I'm writing a program in php where a user gets credits monthly based in their registration date.
I'm writing a cron job that would be run daily.
I need to determinate if a date if a due date
In others words;
If a registered in date 2010-02-10 and today is 2011-07-10 I should get credited.
So the question is how can I deteminate reliably in php that two dates are separated by a whole month.
Or alternatively how can I get the next due date. For example today is 2011-07-18 so the next due date would be 2011-08-10
If you convert your date (fetched from the database, most likely) into a DateTime PHP object, you can use the DateTime class' methods to add exactly one month and compare that date with the current date.
As an example from the PHP documentation, this snippet adds 10 days to a given date.
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";

PHP / MySQL - determine a series of dates based on a start date + INSERT

I would like to produce a list of dates based on a user determined "start date", over a 12 week period.
If a user selects today's date, I need to know each date from today over 12 weeks.
Once I have these dates, I would need to insert them into a DB table.
I'd like to say I've made some in-roads into how to do this, I reckon PHP Mktime (http://php.net/manual/en/function.mktime.php) and a counter might be in right area, but I'm a bit stumped.
Any suggestions or links would be much appreciated, thanks.
EDIT:
Found this which I missed first time around:
PHP: get next 13 dates from date?
That's getting into the right area.
EDIT 2:
From the above listed post this works very well to get any number of dates from a given start date:
$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=42; $i++)
{
array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
I have changed the example above to get dates for 6 weeks (42 days). Does anyone have any clues as to how to modify this to split the 42 days into weeks?
So working with the results in the array, I would like to produce a list of weeks containing the calculated dates - Week 1 (list dates), Week 2 (list dates) etc...?
In many, many, cases, strtotime() can be a lifesaver.
<?PHP
$userdate = strtotime($_REQUEST['whatever']);
$date1 = strtotime('+1 week',$userdate);
$date2 = strtotime('+10 days',$userdate);
$date3 = strtotime('+1 month',$userdate);
$dateN will be a timestamp, which you can just feed to date.

Add 30 days for date in db

I have publish up date and publish down date in my DB.
Currently they are both same dates.
How do I change it (during mysql insert) so publish down date is 30 days past publish up date.
I am using $pubDate
Thanks
You can use DATE_ADD():
DATE_ADD(my_date, INTERVAL 30 DAY)
in php, before inserting you can use strtotime():
if the publishDown date is a timestamp:
$publishDown = strtotime("+30 days",$publishDown);
otherwise you may have to use mktime to get it in the right format

Categories