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.
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);
Check the library here
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}
This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 5 years ago.
When I try to convert very high dates, such as 2045-01-01, I get another date:
date("Ymd", strtotime("2045-02-15"));
I obtain a wrong date
19700101
but when
date("Ymd", strtotime("2017-02-15"));
I have the good date
20170215
I don't understand why? Someone just explain to me what's going on?
that's a unixtimestamp problem (=> https://de.wikipedia.org/wiki/Unixzeit#/media/File:Year_2038_problem.gif), better:
date_parse("2006-12-12 10:00:00");
date_parse_from_format ( 'Ymd' , "2017-02-15" );
or
$date = DateTime::createFromFormat('Ymd', "2017-02-15");
echo $date->format('Y-m-d');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
(I've had to edit this post since John Conde wrongly marked it as answered because he didn't understand my question.)
In my PHP app, the user should be able to enter a date in any format the strtotime() function understands. Here is the input:
<label>Release Date:</label>
<input type="text" name="release_date"/> <span>Use any valid
date format</span>
<br />
Then the index page draws it from the form and stores it in the database like this...
$release_date = $_POST['release_date'];
and displays it on the list page like this:
<td><?php $date = date('n/j/Y', strtotime($product['releaseDate']));
echo $date; ?></td>
I know how to convert dates from one format to another. The display side is fine. But WHERE and HOW do I take the user input - entered in ANY date format the strtotime() function can interpret - and turn it into a readable format going into the database? Right now if I enter any date not in 'xxxx-xx-xx' format it's read as 0 and comes out as UNIX 0, 1/1/1970.
Thank you.
Some dates would work find, and you can do it, but some dates such as anything from the 1st to the 12th of the month would be ambiguous since you could not tell the difference between the month and the day. If you allow two digit years you have an issue with year's 2001 to 2012 as well - AND years 2001 to 2031 with years conflicting with months.
You could ask the user to clarify in certain situations, but that is even more complication.
Don't forget to add:
$todate = stripslashes(trim($_POST['form-todate']));
To kill any potential SQL injections.
Also here is a date validation:
function validateDate($date, $format = 'Y-m-d H:i:s') {
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
function was copied from this answer or php.net
You can pass the input string to this function with various formats until one or more matches (you probably don't want the time in there so take it out).
If there is more than one format matching - query the user to confirm which one.
By the way if you are webifying this there is a great bootstrap js technique I have used at (i think) https://eonasdan.github.io/bootstrap-datetimepicker/
It makes a mini calendar on your input field so the users don't have to type anything - just point and click.
This question already has answers here:
Calculating age from date of birth in PHP
(10 answers)
Closed 8 years ago.
I have a date of birth in the format...
30-01-1983
I am trying to work out how to ensure that at least 16 years have passed since this date. A basic age validation.
It seems to be defeating me but I think I am just overcomplicating it. Does anyone have an example they can point me in the direction of?
Here is a simple method;
<?php
$date2=date("d-m-Y");//today's date
$date1=new DateTime("30-01-1983");
$date2=new DateTime($date2);
$interval = $date1->diff($date2);
$myage= $interval->y;
if ($myage >= 16){
echo "valid age";}
else{
echo "Invalid age";}
?>
Hope it helps
It's pretty easy to do manually, but PHP provides a built-in function that does the heavy lifting for you:
http://www.php.net/manual/en/datetime.diff.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.