Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rojonunoo's avatar

How to continously calculate date and time

so i am looking to doing something like this i have a button and i want it to appear 30 days after its clicked . i am building app and i have a button Pay Staff . and i want this button to appear 30 days after its clicked to pay staff of the organization.

0 likes
3 replies
ciscofan's avatar

In any case you need to store information about "button clicked" event occur. And then check it every time before page is loading.

There is so many ways you can do it.

For example, if you storing data in mysql table button_click_events, then you can count days passed from event by MySQL query:

SELECT DATEDIFF (CURDATE(), button_click_events.event_date);

Btw, you're asking very unspecified question, that's hard to answer without context.

rojonunoo's avatar

@ciscofan ok so i have list of Staff members on a page . Every 30 days staff members have to be paid to their specific accounts . e.g

Name Salary Action John 400 Pay Peter 800 Pay

so after the Pay tag is clicked i want it to be

John 400 Paid 1 minute ago Peter 800 Paid 20 days ago

so after 30 days i want the page to return to

John 400 Pay Peter 800 Pay

i already have relationship and all set up i don't know how to go about this idea i hope is clearer now :-(

ciscofan's avatar

Ok, lets assume you have this relationship in you Staff Model.

public function salary_history()
{
  return $this->hasMany('App\Models\SalaryHistory', 'staff_id');
}

Which says that every staff have its own "salary paid history".

Every time button is clicked it submit a form (or send an AJAX request to us) and we add a row to SalaryHistory table with created_at column.

Which you can easily access before rendering a view.

$latest_salary_history = $staff->salary_history()->orderBy('created_at')->first();

$created_at = $latest_salary_history->created_at;

Now check how many days passed from $created_at:

$event_time = new DateTime($created_at);
$current_time = new DateTime();
$interval = $event_time->diff($current_time);
$days = $interval->format('%a');

if ($days >= 30) {

$button = '<button>Pay</button>';

}else{

$button = 'Already paid '.$days.' days ago';

}

Hope some of this will help.

Please or to participate in this conversation.