Home  >  Article  >  Backend Development  >  How to operate dates and strings in php

How to operate dates and strings in php

墨辰丷
墨辰丷Original
2018-06-08 16:57:352519browse

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(&#39;1986-07-22&#39;); 
?>

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:

Video photography in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn