How to find date differences in php [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);

Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;

If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;

I think you can make use of the date_diff function in PHP.

1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.

Related

Subtracting date in php [duplicate]

This question already has answers here:
Subtracting two dates in php
(7 answers)
Closed 8 months ago.
I'm trying to make a program that subtracts the user's inputted birthdate and the date today.
Here is my code:
$bday = $_POST['bday']; //user input
$today=date("Y-m-d");
$myage= $today - $bday; //i got a warning message here saying "A non-numeric value encountered"
Any idea how to fix this?
Please try this snippet, Assuming you are requiring days and/or years as a result. I used the date_diff function for this code. Converting the user input into date/time( has the Y-m-d format) also I hard coded the user input for easier visualisation.
Working Snippet:
$input="2013-12-12"; #User input
$date = strtotime($input); #Convert user input to date time
$date1=date_create(date("Y-m-d")); #date today
$date2=date_create(date('Y-m-d', $date)); #inputted date
$diff=date_diff($date1,$date2); #date_diff function
echo $diff->format("%Y years or %a days");
Output:
8 years or 3123 days
There is possibly a cleaner version of this. But I believe this is the right flow for your requirements.

PHP short date to long date issue [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm writing some code a script, which has short dates like
14/12/17
30/11/17
20/11/17
I need to convert these to long date format so i used PHP date function and strtotime as below
echo date('d-m-Y',strtotime('14/12/17'));
But it always getting 01-01-1970 as output, but it should be 14-12-2017
anyone know how to convert this to a long date format please.
PS. Other question answers suggest change the date input format, but I cannot change date input since it's getting from another site
This is from the link I posted and OP says is not correct.
Originally posted by ceiroa.
Convert one date format into another in PHP
$myDateTime = DateTime::createFromFormat('d/m/y','14/12/17');
$newDateString = $myDateTime->format('d-m-Y');
Echo $newDateString;
https://3v4l.org/2AcAd
Use preg_replace to flip the day and month on the incoming dates, then use date as you want.
$orig = ['14/12/17', '30/11/17', '20/11/17'];
foreach ($orig as $s) {
echo date('d-m-Y', strtotime(preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$2/$1/$3', $s)))."\n";
}

PHP - Get timestamp X hours from now [duplicate]

This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.

How to check hours in timestamp is in between a particular range of hours? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
I have a timestamp in the format of:
1330559989
How to check the hours in the timestamp is in between 6 hours and 23 hours?
Can someone help me please?
date('H', 1330559989) will give you the hours in 24h format with a leading zero, in this example: 23.
There is also a specific function for the hour of a timestamp called getdate which offers an array interface to that information (demo):
getdate(1330559989)['hours']; # 23
$hrs=date('H', 1330559989);
if ($hrs>= 6 && $hrs<= 23) {
// your code
}
If you want to check the time between now and that given timestamp, then you can use:
<?php
echo date("H", strtotime(now)-1330559989); // Displays the difference from now.
echo date("H", 1330559989); // Displays that time!
?>
Hope this helps! :)

PHP error when using strtotime

Hi I get an error when trying to get date interval using php strtotime function
the code is:
<?php
$interval = time() - strtotime('1992/08/13');
//expect to be 18
// but the output is 1988
print date('Y', $interval);
?>
any advice?
thanks
If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP
There's an example of using it there that should allow you to do what you want to do.
That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.
print $interval / (60*60*24*365.242199);
Are you tring to get the years elapsed rather than the actual year?
If so:
$year = 31556926;
$interval = time() - strtotime('1992/08/13');
print round($interval / $year);
$interval = time() - strtotime('1992/08/13');
These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.
If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out #James C's link and use his solutions. I just wanted to give some explanation.

Categories