Calculate age from given datetime stamp - PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a variable $dob which returns a date, in the following format: 1970-02-01 00:00:00
How can I use php to calculate the age of the person?

Your question is explained in detail in the PHP documentation, it bascially goes like this:
// create a datetime object for a given birthday
$birthday = new DateTime("2012-12-12");
// substract your timestamp from it
$diff = $birthday->diff($dob);
// output the difference in years
echo $diff->format('%Y');

Try this:
<?php
$age = '1970-02-01 00:00:00';
echo (int)((time()-strtotime($age))/31536000);
Output: 43, in years.

$dob = '1970-02-01 00:00:00';
$date = new DateTime($dob);
$diff = $date->diff(new DateTime);
echo $diff->format('%R%a days');
Basically stolen from here: http://uk1.php.net/manual/en/datetime.diff.php
Formatting options: http://uk1.php.net/manual/en/dateinterval.format.php

One-liner:
echo date_create('1970-02-01')->diff(date_create('today'))->y;
Demo.

i would try this
$user_dob = explode('-',$dob);
$current_year= date('Y');
$presons_age = $current_year - $user_dob[0];
This is an untested code but i feel u should get the logic.

strtotime() will convert your date into a timestamp, then subject that from time() and the result is the age in seconds.
$age_in_seconds = time() - strtotime('1970-02-01');
To display the age in years (+- 1 day), then divide by the number of seconds in a year:
echo "Age in whole years is " . floor($age_in_seconds / 60 / 60 / 24 / 365.25);

Related

fetching only year and month from a date saved in the format of strtotime [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My date string value is 1478025000. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be 1477852200
Try something like this:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY
If you want to get just the year and month as an epoch, try something like:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();

How to change date type year in string php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
echo $current_year = date("Y").'<br><br>';
echo $string_year = strftime("%Y", strtotime($current_year));
I want to change date(Year) to string but this code show output as 1970.
I don't understand why it is 1970?
How to make current year that is 2016.
please help.
Your issue is that the strftime function takes a timestamp as the second param but you are passing a string.
=> string strftime ( string $format [, int $timestamp = time() ] )
Thus when you pass date("Y").'<br>' technically this is not a timestamp anymore and this defaults to the 1st year that our php works from being 1970.
When doing
echo $current_year = date("Y");
echo $string_year = strftime("%Y", strtotime($current_year));
I get 2016 but honestly I don't understand why you are asking for the year then changing it to a year again since both $current_year and $string_year are strings.
echo $current_year = date("Y");
echo $string_year = strftime("%Y", strtotime($current_year));
echo gettype( $current_year); //Output: string
echo gettype( $string_year); //Output: string
The entire date should be passed within strtotime(), and not just the year. Or you could simply pass the timestamp as the second parameter.
Try this:
echo $string_year = strftime("%Y", time());
OR
echo $string_year = strftime("%Y", strtotime("now"));

Days left from two given dates php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two dates given, aug 27 2015 -> sep 9 2015.
I'd like to know how much days have passed while using the UTC - 8 timezone.
Can anyone share a small tutorial/code to help my case?
Thank you so much.
You can use DateTime class also. And the DateTime.diff function.
<?php
$d1 = new DateTime();
$d1->setTimezone(new DateTimeZone('America/Los_Angeles')); //Do this for all 3 objects
$d2 = new DateTime();
$d3 = new DateTime();
$d1->setDate(2015, 8, 27);
$d2->setDate(2015, 9, 9);
$gone = $d1->diff($d3);
$left = $d2->diff($d3);
echo "<br>";
echo ($gone->format('%R%a days'))." gone. <br>";
echo ($left->format('%R%a days'))." left. <br>";
?>
After some research and reading some php documentation comments, this code worked for me:
<?php
date_default_timezone_set("America/Los_Angeles");
$date1 = strtotime("27 August 2015");
$days = floor((time() - $date1)/86400);
print("$days days have passed.\n");
?>

How can I calculate the mid-point between two dates in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I calculate the mid-point between two dates in PHP
In Java Script we do -
var midpoint = new Date((date1.getTime() + date2.getTime()) / 2);
Any help would be appreciated.
Try this:
$midPoint = (strtotime($date1) + strtotime($date2))/2;
This is done fairly similarly to javascript actually try this
$midpoint = ($date1 + $date2) / 2
This assuming $date1 and $date2 are timestamps ( the same that is generated from getTime() )
I have found a way for this -
$daysDiff = diffDateTime($StartDate, $EndDate);
$midDaysDiff = round($daysDiff['days']/2);
$midDate = strtotime(date("m/d/Y", strtotime($StartDate)) . "+ $midDaysDiff Days");
function diffDateTime($StartDate, $EndDate)
{
// returns diff between two dates in days...
}

Comparing dates (older, newer, or the same) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Date 1: dd/mm/yy
Date 2: dd/mm/yy
Date 3: dd/mm/yy
Say I have:
$date1 = 04/07/2014;
$date2 = 04/06/2014;
$date3 = 04/07/2014;
What would be the most efficient method to determine if date 2 is between or equal to date 1 and date 3 using PHP? In other words, what is the best way to check if the dates are the same, newer, or older than another date
The best way is to convert date into timestamp, then it will be easy to compare.
Try to use
$time1 = strtotime("dd/mm/yy");
$time2 = strtotime("dd/mm/yy");
Well, this really depends on what you are using this for, what I personally did (when checking dates in a MySQL database for a scheduling website I made) I used the following:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
You could do the same, input your date as Ynj (See PHP documentation on date here)
and then compare them. This may help put this whole idea in context; this is the code I wrote for my website:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
if($now > $publishDate)
{
//This is saying that right now, is greater than when this was added
$msg = "This date is smaller than the time now, which means it is in the past";
}
if ($now < $publishDate)
{
echo "This date is greater than the time now, which means it is in the future.";
}
if ($now == $publishDate)
{
echo "Date is the same";
}
If you add more context to your question I am sure I can help allot more! Good luck

Categories