Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php date count down from join date to one year in the future

Tags:

php

Hello I am currently learning with the latest version of PHP and I am attempting to create a script to perform the following. Capture the date and time a person joins the site; calculate one year from the join date and time ; Then as each new day passes calculate the numbers days remaining before reaching the future date.
This is what I have so far

<?php 
//This top half s working just fine
$datejoin = new DateTime();
echo $datejoin->format('D M j h:i:s A Y') .'<br>';

$dateoneyear = new DateTime();
$dateoneyear->add(new DateInterval('P12M'));
echo $dateoneyear->format('D M j h:i:s A Y') . '<br>';
'<br>';
$datetime1 = new DateTime();
$datetime2 = new DateTime();
$datetime2-> add(new DateInterval('P12M'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days remaining');
?>

This issue I am having is capturing the date value calculated with the “$dateoneyear->add(new DateInterval('P12M'));” and then using the sub method to count down the days.
Of course, I just may be approaching this all wrong but it is what I have been able to pull together. I have been also working with the php.net/manual/en/ to figure out how I can use the sub command but all the examples used there already have a date-inserted i.e 03-25-2009.
Since my date is dynamic, I need to pick up the future value as created and count down from there but I cannot seem to figure the way to do it.

like image 819
cyberfool Avatar asked Dec 31 '25 06:12

cyberfool


1 Answers

The difference required is the number of days between 'now' and an 'expiry' date. The expiry date is 12 months after a given 'join' date.

The number of days remaining is:

$dateJoin = new DateTime('2014/01/01');
$dateExpire = $dateJoin->add(new DateInterval('P12M'));
$now = new DateTime();
$remaining = $now->diff($dateExpire);

$daysRemaining = $remaining->format('%R%a');
echo 'days remaining :', $daysRemaining;
like image 66
Ryan Vincent Avatar answered Jan 02 '26 20:01

Ryan Vincent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!