1

I'm new to php and I wanted to make a contract expiration based on different dates.

I wanted to make a table countdown between different dates that is between start date and expiry date from the user input. The user picks the start date and expiry date.

When the user submits both dates to the database, it will start the countdown timer to years, months and days. Below are the examples.

+------------+---------------------------+------------+
| Start date | Duration                  | Exp. date  |
+----------------------------------------+------------+
| 2016.09.28 | 1 years, 12 months, 4 days| 2018-09-27 |
+------------+---------------------------+------------+

When it reaches the expiry date, it will display expired.

+------------+---------------------------+------------+
| Start date | Duration                  | Exp. date  |
+----------------------------------------+------------+
| 2016-09-28 | Expired                   | 2018-09-27 |
+------------+---------------------------+------------+

Are there any example coding that gets the user input then starts the countdown, displayed on the table? How can I do this in PHP?

Hazirah_Halim
  • 87
  • 2
  • 14

4 Answers4

0
$startTime = '09:00:00';
$endTime = '18:00:00';

$startTimeStr = strtotime($startTime);
$endTimeStr = strtotime($endTime);
$total = $endTimeStr - $startTimeStr;

$now = strtotime(date('H:i:s'));
$remain = $endTimeStr - $now;

echo 'Countdown: '.floor($remain/3600).'h '.floor($remain/60).'min';

A simple countdown code.

ferry
  • 64
  • 4
0

There are only two ways to achieve this

  1. Run cron jobs in your server
  2. Use Triggers in mysql

or

if you want to do manually

UPDATE users SET Duration='Expired' WHERE due_date > CURDATE();

Community
  • 1
  • 1
Siddhartha esunuri
  • 1,094
  • 17
  • 28
0

I am assuming when you are saying you want to display count down in table, you mean to say, Update the record value to 'expired' once it gets expired in the database table

UPDATE DATABASE VALUE WHEN IT EXPIRES:

  • Write a cron job the runs once everyday, it checks if row is expired and updates the value if expired
  • Use CronTab to run the cronjobs

Hope it was helpful!

Sahith Vibudhi
  • 3,207
  • 1
  • 20
  • 25
0

If your are familiar with OPP and composer this solution should help I recommand your use composer to install this PHP Library. It's verry usefull for manipulationg date. I often use it in my PHP Applications. Carbon Library

If your have composer installed on your computer use this open your a consosle and type composer require nesbot/carbon

The next PHP script is tested and works perfect. If this solution is not Good for you so let met know i will provide another

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

/**
 * I consider you have retrieve the begin and expiration date. Your can use those functions to get
 * for both date year,month and date. By example you can do this 
 * $beginDay = date_format(date_create($beginDate),'d');
 * $beginMonth= date_format(date_create($beginDate),'m');
 * $beginYear = date_format(date_create($beginDate),'Y');  
 * 
 * $endDay = date_format(date_create($endDate),'d');
 * $endMonth= date_format(date_create($endDate),'m');
 * $endYear = date_format(date_create($endDate),'Y');
 * 
 * So you can replace first and second variables by this
 * $first = Carbon::create($beginYear, $beginMonth, $beginDay);
 * $second = Carbon::create($endYear, $endMonth, $endDay);
**/


$first = Carbon::create(2016, 7, 28);
$second = Carbon::create(2017, 7, 28);
$between = Carbon::now()->between($first, $second);
if(!$between === true){

    $diff = Carbon::create($first->year, $first->month, $first->day)->diff($second);

    $year = $diff->y;
    $month = $diff->m;
    $days = $diff->d;

    $Duration = "$year years, $month months, $days days";

    print $Duration;
    //You can update  in your DB
}else{
    $Duration = 'Expired';

    print $Duration;
    //You can update in your DB
}

?>

You can make a cron Job task than will run the script every day. Or simply run this every time a User trigger a specific action based on your context.

Ulrich Dohou
  • 1,307
  • 10
  • 23