This article mainly introduces the method of operating dates and strings in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The example in this article describes the method of calculating age based on birthday in PHP. The details are as follows:
<?php function birthday($birthday){ $age = strtotime($birthday); if($age === false){ return false; } list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); $now = strtotime("now"); list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); $age = $y2 - $y1; if((int)($m2.$d2) < (int)($m1.$d1)) $age -= 1; return $age; } echo birthday('1986-07-22'); ?>
Let’s take a simpler one, which doesn’t feel as high as the above
<?php echo birthday("1989-01-25"); function birthday2($birthday){ list($year,$month,$day) = explode("-",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($day_diff < 0 || $month_diff < 0) $year_diff--; return $year_diff; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Use php questionnaire results statistics
How to use php to automatically execute .sql files
The above is the detailed content of How to operate dates and strings in php. For more information, please follow other related articles on the PHP Chinese website!