PHP Date : get date different in years - php

this is my code:
date_default_timezone_set('Asia/Kuala_Lumpur');
$date_join = $row['date_joined']; =produce 2012-09-03
$today = date("Y-m-d"); = produce 2014-08-29
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H'.$a, **$xxx**);
how can i get date durations ($today - $date_join) in years like :
Date of services : 1.5 years

You can use PHP strtotime() function. Try like this..
$join_date = '2012-09-03'; //join date
$today = date("Y-m-d"); //current date
$date_join = strtotime($join_date); // join date to seconds
$today = strtotime($today); //current date to seconds
$differenceInSeconds = $today - $date_join; // Time difference in seconds
echo number_format($differenceInSeconds / (365 * 24 * 60 * 60), 2) . ' Year(s)';
Get time difference in seconds from two dates
Divide the difference by one year equivalent seconds

Related

PHP subtract unknown time from datetime

So, I'm creating a booking system. When I retrieve this booking from the database, I need to check if the current date and time is closer to the actual booked date and time.
On the admin dashboard, the admins specify how much time earlier the client can make a checkin, let's say for example, 30minutes. But this time can be different. Can be 1hour, 2hours, 10minutes.
When I get the result from the database I get them like this:
$date_schedule = '2021-03-25 15:40:00'; // Can be any date in the future as well;
$time_to_check = '00:30:00'; // Can be '01:05:00', whatever the admins set as time_to_check;
// Expected result
'2021-03-25 15:10:00';
I tried subtracting this but I didn't made it work.. This is what I did.
$current_date = date("Y-m-d H:i:s");
$booking_date = '2021-03-25 15:00:00'; // From database
$time_to_check = '00:30:00'; // From database
$hours = explode(':', $time_to_check);
$data_check = date($booking_date, strtotime('-' . $hours[0] . ' hour -' . $hours[1] . ' minutes'));
But with this, $data_check returns the same value as $booking_date, it's not subtracting the time.
Convert your date to DateTime to make some operations on it :
function changeDate($date, $interval) {
$datetime = new DateTime($date);
$values = explode(':', $interval);
$datetime->modify("-$values[0] hours -$values[1] minutes -$values[2] seconds");
return $datetime->format('Y-m-d H:i:s');
}
$date = changeDate('2021-03-25 15:00:00', '00:30:00');
echo $date; // 2021-03-25 14:30:00
$date = changeDate('2021-03-25 15:00:00', '01:30:00');
echo $date; // 2021-03-25 13:30:00
$date = changeDate('2021-03-25 15:00:00', '02:30:15');
echo $date; // 2021-03-25 12:29:45
You can find documentation here https://www.php.net/manual/en/book.datetime.php
Just convert the timestamp to Unix time, and then subtract 30 minutes in seconds (30 * 60) from the Unix time.
Like this:
$date = '2021-03-25 15:10:00';
$timestamp = strtotime($date);
$timestamp = ($timestamp - (30 * 60));
echo gmdate("Y-m-d H:i:s", $timestamp);
EDIT: If you are in an odd timezone, like me (GMT+0100) add or subtract difference;
$timestamp = (($timestamp - (30 * 60)) + (1 * 60 * 60))

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

How to get date when given a date and days from that date? php

I have to get a date in the future.
I have a date in the past and I need to add "X" days and get the resulting date.
Here is an example of what I want to do
2016-02-28 09:07:22 + 62 days = NewDate
I am using PHP
You have to use strtotime() function to convert the date string into seconds, then add the days, and then convert back with date() function if you want. The code will look like this:
$dateString = '2016-02-28 09:07:22';
$time = strtotime($dateString); //convert to seconds
$timeNew = $time + 62 * 24 * 60 * 60; //add 62 days in seconds
$dateNew = date("Y-m-d H:i:s", $timeNew); //convert back
If you are using the \DateTime() object, you can do something like:
$past = new \DateTime('2016-02-28 09:07:22');
$interval = new \DateInterval('P64D'); // 64 days
$future = $past->add($interval);

retrive date after 1.3 year from the given date in php

I am working on date and I am stuck at a point. How can I get the date after 1.2 or after 1.5 year from the given date?
My code is as follows:
$date = date("Y-m-d", strtotime($from_date . ' +'.$valid_duration.' '.$day) );
where $valid duration can be number as 1, 2, 1.2, etc. and $day is year, months, days.
To get the future date try this:
$StartingDate = date('Y-m-d'); // todays date as a timestamp
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StartingDate)) . " + 1 year 2 months 5 days"));
Hope this helps.
Not sure if you can define years/months/days with a decimal point.
But regardless, have you considered just using the timestamp?
86400 seconds in a day, so say you wanted to get date from one year in the future, you could use something like:
$thetime = time() + (365 * 86400);
$date = date("Y-m-d", $thetime);

php how to to do operation on date

I have problem when i want calculate date. Simple Example:
I have 2013-09-01 is start date and I have 30day per month. My work i need alert tell to my user in 10 day before end month(it's mean on 2013-09-20i must alert message it's 10day more for end of this month). So every one have any idea for help to calculate it. becuese i like can't (+, -, *,/) on date. Now i am some data like
<?php
date_default_timezone_set('Asia/Phnom_Penh');
$current = time();
$start = 1380188957;
echo 'Start date: '. date('Y-m-d', $start) ."\n";
echo '<br/>';
$repeat = 30;
$enddate = time() + ($repeat * 24 * 60 * 60);
echo 'end date: '. date('Y-m-d', $enddate) ."\n";
Thanks in advent for helping.
Not every month has 31 days, you can get the number of days in any month by using the t option for the string format param in php's date() function.
// Current time as unix timestamp
$now = time();
// Number of days in current month
$days_this_month = date("t", time());
// Last day of the current month as a unix timestamp;
$end_of_month = strtotime(date("Y-m-t", time()));
// Ten days before the end of the month as a unix timestamp
$ten_days = strtotime('-10 days', $end_of_month);
Now we can do a check to see if it is 10 days before the end of the month:
if($now > $ten_days) {
// Do something
}
$start = 1380188957;
$enddate = time() + ($repeat * 24 * 60 * 60);
That's your own code. Using that we can easily compute 10 days before end date
$alert=$enddate-864000; // That's 10 days
$alertdate=date('Y-m-d', $alert);

Categories