-1

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";
}
Arun Kumar
  • 111
  • 2
  • 3
  • 14

3 Answers3

0

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);
A.D.
  • 2,352
  • 2
  • 15
  • 25
0

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

xinaris
  • 454
  • 1
  • 4
  • 16
  • Thanks, for the reply, but I have problem with, if record fill date is less then 7th of next month then, update button will show. – Arun Kumar Nov 06 '17 at 07:40
  • The problem is that i don't know exactly your situation to answer correctly. So you have in your database for example records until 20th of December. You want to be able to be able to edit data until 7th of December. But if you have data in the database until 1st of December you want to be able to edit until then? – xinaris Nov 06 '17 at 07:51
  • I explain I have data in the database of each month which is inserted every day, I want the data of October month will be able to edit till 7th of November and so on. After 7th of next month, data won't be able to edit. Hope you understand my query. – Arun Kumar Nov 06 '17 at 07:57
0

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.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38