This question already has answers here:
PHP calculate age
(36 answers)
Closed 5 years ago.
I have a PHP form which includes 3 fields for person birthday:
iBirthday, iBirthMonth (int 1 thru 12), and iBirthYear (int 4 digits)
I am trying to calculate if the person is older than 18 years, when pressing submit, before running the SQL INSERT.
How can i calculate this? I have no idea how to concatenate these 3 int into a date, and compare to current date.
I have some tests that work, for example
//Check if gender is NULL
if ($iGender == '') {
$sTopError = gettext('Did not select Gender');
$bErrorFlag = true;
}
I would like to do a similar test, that says: "Person not above 18 years".
This function will return amount of years from given date in YYYY-MM-DD format:
function get_age($date)
{
$today = date('Y-m-d');
$today = new DateTime($today);
$date = new DateTime($date);
$diff = $date->diff($today)->format("%Y");
return (int)$diff;
}
Usage:
echo get_age('2000-02-01');
Related
This question already has answers here:
Calculate months, years and days between two given dates as timestamp [duplicate]
(4 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
Here is my code:
<?php
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>
I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.
Yes php has something built in for that :)
$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');
This question already has answers here:
How to compare two dates without years in php? [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with date .
I have a Birth date eg. 1993-06-01 in database. And now i want to compare with now date so, I can pop up the birthday message. How is this possible to compare with only month and day field. Especially in query so i can attached their name with birthday message
Used framework Pyrocms.
For PHP just use DateTime.
Example:
$date1 = new DateTime('1993-06-01');
$date2 = new DateTime();
if ($date1->format('m-d') === $date2->format('m-d')) {
// send the birthday message
}
If you want to do this in SQL then use MONTH() and DAY().
Example:
$sql = "SELECT * FROM `table` WHERE MONTH(`date_column`) = " . $date->format('n') . " AND DAY(`date_column`) = " . $date->format('j');
If you need to compare with month, you have this function in mysql:
MONTH(date)
That returns the month (in numbers 1 .. 12)
You also have
MONTHNAME(date)
that returns the month in string ('January', ' February', ...)
For the day you have:
DAYNAME(date) --> returns ('Sunday','Monday' ...)
DAYOFWEEK(date) --> returns (1 = Sunday, 2 = Monday, …, 7 = Saturday)
You can look for all the date time functions in this link MySql dev
This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want check whether the date from variable is older than current date about 7 days or more.
Now, I check whether this date is -1 day from current:
$old_email = strtotime($result->repairs_date_received) >= strtotime('-1 day') ? true : false;
UNIX time is in seconds, so you can simply check using basic operators like > and <.
$week_ago = strtotime('a week ago');
$check = strtotime($result->repairs_date_received);
if($check > $week_ago){
// date is newer than week
} else {
// date is older than week
}
This question already has an answer here:
Difference between 2 dates in seconds [duplicate]
(1 answer)
Closed 8 years ago.
date_default_timezone_set('America/New_York');
$search_date = '2012-12-19 13:22:00';
$right_now = date('Y-m-d H:i:s');
$search_date = new DateTime($search_date);
$right_now = new DateTime($right_now);
$interval = $search_date->diff($right_now);
echo $interval->format('%R%s seconds');
This displays how many seconds are different between the search date and right now.
I would expect it to return more than a two digit value because there is more than a 99 second difference between the two dates, so I am not sure what I am doing wrong.
Alternatively, with very little change to your original code:-
date_default_timezone_set('America/New_York');
$search_date = new DateTime('2012-12-19 13:22:00');
$right_now = new DateTime();
$seconds = $right_now->getTimestamp() - $search_date->getTimestamp();
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate age
I'm trying to subtract the years and check the user's age. I keep getting 1969 for $user_birth, which I think the raw date can't be parsed.
$raw_birth = "01-19-1980";
$user_birth = date("Y", strtotime($raw_birth));
$today_date = date("Y", time());
echo $raw_birth."<br />".$today_date."<br />".$user_birth."<br />";
echo $today_date-$user_birth;
Any ideas?
$year="1997";
$month="01";
$day="24";
$age=date("Y");
$age-= $year;
if(($month>=date("m")) && ($day>date("d"))) {
$age--;
}
This code calculates an accurate age of an user.