subtract two dates in php [duplicate] - php

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.

Related

Get day number from Date format by php [duplicate]

This question already has answers here:
How to extract only 'Day' value from full 'Date' string?
(7 answers)
Closed 2 years ago.
I have a date Formate from database number 2020-11-15 I want to get only the day number. I tried the following.
$your_date = '2020-11-15';
$ar_day2 = date("D", strtotime($your_date)); //The value is (Sun)
I am looking for result '15'.
Is there way by php?
use small d
$ar_day2 = date("d", strtotime('2020-11-15'));
echo $ar_day2 = date("d", strtotime('2020-11-15')); // will give => 15
echo $ar_day2 = date("D", strtotime('2020-11-15')); // will give => Sun

How can I calcualte Age in Years, Months and Days [duplicate]

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');

How to calculate time difference in php or laravel [duplicate]

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 time like total worked time and total duty time. eg.
$dutyTime= 182:00:00; ("H:i:s")
$workedTime= 178:10:28; ("H:i:s")
my question is that how can i get difference between this two time;
Expected Output
$duration=03:49:32; ("H:i:s")
i tried strtotime() but i takes upto 24hrs.
<?php
$start = date_create('21:21:05'); //start time
$end = date_create('10:20:00'); // end time
$diff = date_diff( $start, $end );
$date= $diff->h.':'.$diff->i.':'.$diff->s;
echo date("H:i:s",strtotime($date));
?>

How do I compare dates in PHP? [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 8 years ago.
How can I check if a date captured in the database is less than the current date?
foreach( $results as $r ) {
$date = $r['date'];
if($date < $currentdate) {
echo 'oldest dates';
} else {
echo 'newest dates';
}
Use strtotime function:
$date = strtotime($date);
$currentdate = strtotime($currentdate);
Use strtotime($date)<strtotime($currentdate)
and make sure that both dates are in correct format.

Converting month name in given date into month number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date format converting
I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?
You can try with strptime
$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');
here's a Codepad
Try this
Change
<?php echo date('Y-m-d',strtotime('2012-september-09'));?>
To
<?php echo date('Y-m-d',strtotime('09-september-2012'));?>

Categories