How to calculate the number of years married? - php

I have a line of text as follows...
"Now married for XXX years, my wife and I are excited for the future."
Our wedding date is: June 7th, 2008
So, currently it reads:
"Now married for two years, my wife and I are excited for the future."
What php code can I put in the XXX space to automatically insert the number of years we have been married?

<?php
$married = new DateTime('2009-10-11');
$currentdate = new DateTime();
$interval = $married->diff($currentdate);
echo $interval->format('%y years');
?>
Something along those lines should do the trick. I haven't run this code, so you may need to tweak it a bit but it should get you started.

What you can do is subtract the current date and the anniversary date. use a function like one that is displayed here:
http://www.ozzu.com/programming-forum/subtracting-date-from-another-t29111.html

<?
$anniv = new DateTime('June 7th, 2008');
$now = new DateTime();
$interval = $now->diff($anniv, true);
echo 'Now married '.$interval->y.' years';
?>

The DateTime objects that people have used so far only work if you have a newer version of PHP. Here's a solution without them. Finding just the number of years is pretty easy.
<?php
$date1 = strtotime("June 7th, 2008"); //Get general timestamp for day
$today = time(); //Today's date
$secs = $today - $date1; //Get number of seconds passed since $date1 til $today
$years = floor($secs / (60*60*24*365)) //Derive years from seconds using number of seconds in a year
echo $years;
?>
or, in a single line of code:
<?php
$years = floor((time() - strtotime("June 7th, 2008")) / (60 * 60 * 24 * 365));
?>

Related

Days spent between now and n date [PHP]

Okay! so we all have seen how to get days remaining between NOW and a DATE in the future Like I have in my simple code below:
SAY: $endate = a date in future (5 days from today);
$start = new DateTime();
$end = new DateTime($enddate);
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Remaining'; // 5 days Remaining
The above PHP Code is expected to show you how many days are left between NOW and the FUTURE DATE.
But what I want is the reverse of this situation. That is.
instead of having
5 Days Remaining
I need some thing like
0 Day(s) Spent //where today is day 0 of 5
$start = new DateTime("2021-12-14");
$now = new DateTime();
$now_diff = $now->diff($start)->format("%a");
print_r($now_diff .' Day(s) Spent');
Finding the number of days between two dates
CLUE TO ANSWER PROVIDED BY #KHIMAJI VALUKIYA in comment
The trick here is to set the start date not as current date but the actual date to start counting/calculating from.
Then the end date should be set to the current date.
$start = new DateTime($enddate); //date to start counting from
$end = new DateTime(); //current date
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Spent'; // 5 Days Spent

Calculate age In Years and Days PHP

I know this has been asked many times over the years, but I still get different results than what I think should be right. I have the following code that calculates the amount of days between two dates and then it converts it to years and days. When I convert result isn't what I expect. See below. Please let me know what is incorrect, this is really frustrating.
Thanks!
$born = '1985-09-09';
$date = date('Y-m-d H:i:s'); // this is today's date
$birthdate = new DateTime("$born");
$today = new DateTime("$date");
$diff = $today->diff($birthdate)->format("%a");
$days = $diff;
$years_remaining = intval($days / 365); //divide by 365 and throw away the remainder
$days_remaining = $days % 365;
echo "<b>Age:</b> ".$years_remaining."y-".$days_remaining."d<br />";
What I want to appear:
Age: 35y-0d
But what I get:
Age: 35y-9d
Because leap years contains 366 days you can't just divide days/365:
<?php
$born = '1985-09-05';
$date = date('Y-m-d H:i:s'); // this is today's date
$birthdate = new DateTime("$born");
$today = new DateTime("$date");
// get diff in full years
$diff_years = $today->diff($birthdate)->format("%y");
// add years diff to birthday, (so here your last birthday date)
$birthdate->add(new DateInterval("P{$diff_years}Y"));
// count days since your last birthday party day
$diff_days = $today->diff($birthdate)->format("%a");
echo "Age: ".$diff_years."y and ".$diff_days." days ";
Here you can try live PHP code

PHP Date - Time difference formatting

I have a date and time saved in a database, in the following format: 2016-04-03 12:54:11
Basically, this date and timestamp represents the exact date and time something was created. What I'm trying to do is display a second date and time, that is the exact number of days since the first timestamp.
So if the timestamp in the dataase was 2016-04-03 12:54:11 and todays date and time is 2016-04-04 12:54:11, it would display: Overdue by: 1 Day
So far I have:
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = time();
$dateDifference = abs(strtotime($dateCreated) - strtotime($dateNow));
$years = floor($dateDifference / (365*60*60*24));
$months = floor(($dateDifference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($dateDifference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); ?>
And then:
echo "<strong>Overdue by:</strong> $days Days.";
however, this code displays that 2 dates, only a day apart, as 14 days apart.
Help would be appreciated, cheers.
Try using DateTime objects. They'll make your life much easier.
The above could then be achieved with the following:
$date1 = new DateTime('2016-04-03 12:54:11');
$date2 = new DateTime('2016-04-04 12:54:11');
$diff = $date1->diff($date2);
The $diff variable will then be a DateInterval object, which has a days property to show the amount of days between the two dates. In this case 1, so you can do:
echo "Overdue by: " . $diff->days . " days.";
Which will output Overdue by: 1 days..
On a side-note: You should really not be using mysql_ functions anymore. They have been deprecated for years and are no longer part of the PHP core in the latest PHP version. So this code will not work on an up-to-date server. Also see: Why shouldn't I use mysql_* functions in PHP?
I think the best way to do it is using the Php DateTime class witch allow us to do it.
Try this:
$datetime1 = new DateTime("YOUR_DB_TIMESTAMP");
$datetime2 = new DateTime("today");
$difference = $datetime1->diff($datetime2);
echo difference->d;
Also, you can choose the way that you want to show the diference.
$diff->format('%R%a days')
Reference:
Example-> Finding the number of days between two dates
DateTime Doc -> http://php.net/manual/es/class.datetime.php
Regards!
I would use the DateTime object for things like this, because are really a very big help.
$dateCreatedObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateCreated);
$dateNowObj = new DateTime();
$dateDifference = $dateCreatedObj->diff($dateNowObj);
echo $dateDifference->format('Overdue by: %a Day');
Please note that this does not handle timezones, multilingual and plural issues.
Diff function returns the difference between two DateTimeInterface objects.
You can try to use DateTime object :
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = new DateTime();
$dateOther = new DateTime($dateCreated);
$interval = $dateNow->diff($dateOther);
echo $interval->format('%R%a days');

How many days left till any date? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

Categories