>  기사  >  백엔드 개발  >  시간 함수 날짜와 PHP에서 일반적으로 사용되는 시간 계산에 대한 자세한 그래픽 설명

시간 함수 날짜와 PHP에서 일반적으로 사용되는 시간 계산에 대한 자세한 그래픽 설명

墨辰丷
墨辰丷원래의
2018-05-22 15:42:121726검색

이 글은 주로 시간 함수 날짜 관련 지식과 PHP에서 일반적으로 사용되는 시간 계산을 소개하며 이는 매우 좋은 참고 가치를 가지고 있습니다. 에디터와 함께 살펴볼까요

프로젝트에서는 오늘, 어제, 이번 주, 이번 달, 이번 분기, 올해, 지난주, 지난 달, 지난 분기 등의 타임스탬프를 사용해야 합니다. 시간이 넉넉한 최근의 장점을 살려 PHP 관련 시간 지식 포인트를 정리해서 공부할 예정입니다

1. PHP 매뉴얼 날짜 함수를 읽어보세요

자주 사용하는 시간 함수:

   checkdate()는 시간이 올바른지 확인합니다

  date_default_timezone_get() 획득 현재 스크립트에서 사용하는 시간대

  date_default_timezone_set()는 스크립트에서 사용하는 시간대를 설정하거나 ini_set()에서 구성을 수정할 수도 있습니다. file

  date_sunrise() date_sunset()은 주어진 날짜와 위치의 일출 시간과 일몰 시간을 반환합니다

   date()는 날짜 형식을 지정하며 자세한 내용은 아래에 제공됩니다

  getdate()는 날짜 및 시간과 관련된 정보를 얻습니다

  gettimeofday()는 현재 시간과 관련된 정보를 얻습니다

  idate()는 현지 시간 날짜를 정수로 형식화하지만 매개변수로 한 문자만 허용합니다.

  Microtime()은 현재 타임스탬프와 초를 반환합니다

  mktime()은 날짜의 타임스탬프

  strtotime()은 영어 텍스트의 날짜 초를 타임스탬프로 구문 분석합니다

2. 중요한 기능에 대한 자세한 설명

date()는 날짜 형식

  string date( string $format [, int $timestamp] )

  디 월의 날, 즉 숫자, 앞에 0이 붙습니다. 예: 01,02

   D 요일, 즉 영어 요일의 약어인 Mon to Sun

 l (L 소문자) 요일, 이것은 완전한 영어 형식입니다, Sunday to Saturday

 N 숫자를 사용하여 요일을 나타냅니다. 1은 월요일, 7은 일요일입니다

S

  W  연중의 주

  F  월, 완전한 영어 단어

  m  월 숫자 형식, 앞에 0

 M

  t  해당 달에 있어야 하는 일 수

 L  윤년인지 감지 , 윤년은 1이고, 월은 0

  Y  4자리 숫자로 표현되는 연도

  y  2자리 숫자로 표현되는 연도

  a  오전 또는 오후 값은 소문자

 A 수도 오전 또는 오후 값

 g 12시간제, 선행 없음 0

 G  24시간제, 앞에 0

 h  12시간제, 앞에 0

 H 24시간제, 앞에 0

 i 일광 절약 시간인가요? , 0机 T가 위치한 시간대가 아닙니다

C 2017-05-08T 15: 22: 21+00: 00 형식

U.) 기능 상세 설명

날짜와의 차이점은 이 기능입니다. date()는 하나의 매개변수만 전달할 수 있으며, date()는 여러 매개변수를 전달할 수 있습니다

 B

H

  s  Seconds

t 숫자

 Y 연도 4자리 숫자

 Z 연도의 날짜

 Z  초 단위의 시간대 오프셋

strtotime() 함수 연결

사용 예

      strtotime ("now");

      strtotime ("10 September 2017");

      strtotime ("+1 day");

      strtotime ("+1 week");

      strtotime ("+1 week 2 days 4 hours 2 seconds");

      strtotime ("next Thursday");
      strtotime ("last Monday");

3, 일반적인 시간 요약

$times = [];
function makeTime(){
  //获取今日开始时间戳和结束时间戳
  $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  $times['today']['begin'] = $beginToday;
  $times['today']['end'] = $endToday;

  //获取昨日起始时间戳和结束时间戳
  $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
  $times['yesterday']['begin'] = $beginYesterday;
  $times['yesterday']['end'] = $endYesterday;

  //获取上周起始时间戳和结束时间戳
  $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
  $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
  $times['lastWeek']['begin'] = $beginLastweek;
  $times['lastWeek']['end'] = $endLastweek;

  //获取本月起始时间戳和结束时间戳
  $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
  $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
  $times['thisMonth']['begin'] = $beginThismonth;
  $times['thisMonth']['end'] = $endThismonth;

  //获取本周开始时间和结束时间,此例中开始时间为周一
  $defaultDate = date('Y-m-d');
  $first = 1;
  $w = date('w',strtotime($defaultDate));
  $beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days');
  $endWeek = $beginWeek + 6*24*3600-1;
  $times['thisWeek']['begin'] = $beginWeek;
  $times['thisWeek']['end'] = $endWeek;

  //获取上月的起始时间戳和结束时间戳
  $beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
  $endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));
  $times['LastMonth']['begin'] = $beginLastmonth;
  $times['LastMonth']['end'] = $endLastmonth;

  //获取今年的起始时间和结束时间
  $beginThisyear = mktime(0,0,0,1,1,date('Y'));
  $endThisyear = mktime(23,59,59,12,31,date('Y'));
  $times['thisYear']['begin'] = $beginThisyear;
  $times['thisYear']['end'] = $endThisyear;

  //获取上年的起始时间和结束时间
  $beginLastyear = mktime(0,0,0,1,1,date('Y')-1);
  $endLastyear = mktime(23,59,59,12,31,date('Y')-1);
  $times['lastYear']['begin'] = $beginLastyear;
  $times['lastYear']['end'] = $endLastyear;

  //获取本季度开始时间和结束时间
  $season = ceil((date('n'))/3);//当月是第几季度
  $beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
  $endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
  $times['thisSeason']['begin'] = $beginThisSeason;
  $times['thisSeason']['end'] = $endThisSeason;

  //获取上季度的起始时间和结束时间
  $beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y'));
  $endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
  $times['lastSeason']['begin'] = $beginLastSeason;
  $times['lastSeason']['end'] = $endLastSeason;

  return $times;
}
$times = makeTime();

目前是我之前用到的时间戳,后期还会积累汇总,避免重复造轮子。

相关推荐:

php date函数介绍与使用方法详解

关于PHP MySQL Update语句的相关内容

PHP验证码类ValidateCode

위 내용은 시간 함수 날짜와 PHP에서 일반적으로 사용되는 시간 계산에 대한 자세한 그래픽 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.