Introduction to PHP Tutorial date and time Operational skills Summary (format, validation, acquisition, conversion, calculation, etc.) _php tips

Source: Internet
Author: User
Tags current time echo date explode getdate local time php tutorial

This example describes the PHP date and time manipulation techniques. Share to everyone for your reference, specific as follows:

demo1.php

<?php
 //Verification Time
 //checkdate () 1. Month 2.3 year
 //checkdate () to determine whether this date is a valid date
 //illegal date, try
 if ( Checkdate (7,16,2010)) {
  echo ' This date is legally valid ';
 } else{
  Echo ' This date is illegal. ';
 }
? >

demo2.php

<?php
 //date--format a local time/date
 //date (), thoroughly study
 //date () can hold two parameters, the first parameter is the format of date and time, [second parameter is timestamp]
 / /y represents a four-digit year, Y represents a two-digit year
 //m the month abbreviation for English, M is the month of the Arabic numerals, and the
 //d of the week in English indicates the day of the Arabic numerals.
 The first parameter can be formatted with a number of unimportant strings
 ////As long as the irrelevant string is no longer in the format directory, it will not be recognized
 //echo date (' Now is: y-m-d '); Now the date is: 2015-04-20//moment
 = H means 24 hours of hours,
 //Clearly is 19, why show 11 points, East eight area, difference 8 hours
 //Now without any settings, so time in the default timezone
 / /echo date (' Now is: y-m-d h:i:s ');
 The emphasis is on the day of the month, time of the second
 echo date (' R ');
 echo Date (' Today's date is: y-m-d H:i:sa ');
? >

demo3.php

<?php
 //Get current time, return an array
 //"SEC"-number of seconds since the Unix era
 //"USEC"-microseconds
 //"Minuteswest"-number of minutes to the west of Greenwich
 // Dsttime "-Daylight Saving Time modified type
 //print_r (Gettimeofday ());
 The element of the first array is the timestamp
 //gettimeofday () is the timestamp
 //$a = Gettimeofday () of the current time obtained;
 SEC gets the time stamp of the current time//
 Convert the time//second parameter that the adult can understand,
 for this example, put and not put, is the same.
 //echo Date (' y-m-d h:i:s ', $a [' sec ']);
 Print_r (gettimeofday (0));
 echo gettimeofday (1);
? >

demo4.php

<?php//Convert timestamp to the time the
 adult can see the
 second parameter of the//date () function is the timestamp
 ///If the second argument is omitted, then the current time is returned
 //If the second argument is not omitted, Then return that timestamp time of
 echo date (' y-m-d h:i:s ', 24554457865);
? >

demo5.php

<?php
 //getdate () can also convert timestamp
 //print_r (getdate ());
 Array ([seconds] => [minutes] => [hours] => [Mday] => [wday] => 1 [Mon] => 4
 //[year] => 2015 [yday] => 109 [weekday] => Monday [month] => April [0] => 1429526066)
 $t = getdate ();
 echo $t [' Year '];
 Pass a timestamp
 print_r (getdate (1029526066));
? >

demo6.php

<?php
 //Direct Get the current timestamp
 //echo time ();//1429526328//
 this time () can be adjusted to date/
 //You can find times () very useful, Can past now and future
 echo date (' y-m-d h:i:s ', Time () +60*60*8);
? >

demo7.php

<?php
 //Get timestamp for a specified time
 //This is the current timestamp//echo time
 ();
 I want to get 2008-08-08 08:08:08
 $beijing 2008 = mktime (8,8,8,8,8,2008);
 echo Date (' y-m-d h:i:s ', $beijing 2008);
? >

demo8.php

<?php
 //use timestamp to calculate time difference
 $now = times ();//current timestamp
 $wnow = mktime (0,0,0,8,16,2016);
 Two timestamp subtraction can be obtained by the difference sec
 echo Round (($wnow-$now)/60/60,2). ' The difference between these few hours ';
? >

demo9.php

<?php
 //Converts a human-readable time, a string form, to a timestamp
 $a = strtotime (' 2010-7-16 15:15:15 ')-strtotime (' 2010-7-16 15:14:15 ');
 if ($a >=) {
  echo asked this gentleman to take a break. ';
 } else{
  echo $a;
 }
? >

demo10.php

<?php
 //Get current file modification timestamp
 echo date (' y-m-d h:i:s ', Getlastmod ());
? >

demo11.php

<?php
 //configuration system environment variable
 echo date (' y-m-d h:i:s ');
 Echo ' <br/> ';
 I started setting the time zone
 putenv (' Tz=asia/shanghai ');
 echo Date (' y-m-d h:i:s ');
? >

demo12.php

<?php
 //putenv (' Tz=asia/shanghai ');
 Gets the current time zone,
 echo date_default_timezone_get ();
 Echo ' <br/> ';
 Start configuring the default time zone
 date_default_timezone_set (' Asia/shanghai ');
 echo Date (' y-m-d h:i:s ');
 Echo ' <br/> ';
 Echo Date_default_timezone_get ();
? >

demo13.php

<?php
 date_default_timezone_set (' Asia/shanghai ');
 "Tm_sec"-seconds
 //"tm_min"-minutes
 //"Tm_hour"-hours//"
 tm_mday"-Day of the Month//"
 Tm_mon"-months of the year, starting from 0 January
 //"Tm_year"-year, starting from 1900
 //"Tm_wday"-Day of the Week//"
 tm_yday"-Day of the Year
 //"TM_ISDST"-Daylight saving time is currently in effect C23/>print_r (LocalTime (Time (), true));
 Array ([tm_sec] => Notoginseng [tm_min] => [tm_hour] =>//[tm_mday
 ] => [Tm_mon] => 3 [tm_year] => ;
 //[tm_wday] => 1 [tm_yday] => 109 [TM_ISDST] => 0)
?>

demo14.php

<?php
 //return time stamp and microseconds
 //How to calculate the page run load time
 //page open time to get a time/
 //page end time to get a time/
 //////////////////back Timestamp////////// Then it is running time
//list ($a, $b) =explode (", Microtime ());
echo $b;
 function fn () {
  list ($a, $b) =explode (", Microtime ());
  return $a + $b; Returns the exact number of seconds
 }
 //When the page is open, get a time
 $start _time = fn ();
 For ($i =0 $i <10000000; $i + +) {
  //
 }
 //page at the end, get a time
 $end _time = fn ();
 Echo Round (($end _time-$start _time), 4);
? >

For more information on PHP related content readers can view the site topics: "The PHP date and time usage summary", "PHP array Operation technique Encyclopedia", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introduction Course", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.