Date and time of PHP

Source: Internet
Author: User
Tags echo date php script

One, Unix timestamp

Represents Greenwich Mean Time in a 32-bit integer. The UNIX timestamp is the number of seconds that have passed since the start of the January 1, 1970 0 o'clock to the current time.

1. Convert date and time to UNIX timestamp

(1) mktime () function

<?pph

echo Date ("Y-m-d-h-m-s", Mktime (12,10,56,12,67,2016)). " \ n ";

?>

(2) Strtotime () function

<?php

echo Date ("Y-m-d", Strtotime ("Now")). " <br/> ";

echo Date ("Y-m-d", Strtotime ("2014")). " <br/> ";

echo Date ("Y-m-d", Strtotime ("+2 Day")). " <br/> ";

echo Date ("Y-m-d", Strtotime ("last Monday"));

?>
(3) Example: Use strtotime () function to write an anniversary countdown program

<?php

$now =strtotime ("Now");

$endtime =strtotime ("2018-6-19 23:59:59");


$second = $endtime-$now;

$year =floor ($second/60/60/24/365);


$temp = $second-$year *365*24*60*60;

$month =floor ($temp/60/60/24/30);


$temp = $temp-$month *30*24*60*60;

$day =floor ($temp/60/60/24);


$temp = $temp-$day *24*60*60;

$hour =floor ($temp/60/60);


$temp = $temp-$hour *60*60;

$minute =floor ($temp/60);


$second 1= $temp-$minute *60;


echo "graduated from college and". $year. " Year ". $month." Month ". $day." Day ". $hour." Hours ". $minute." ". $second 1." Seconds ";


2. Calculation of dates

<?php

In the PHP script receives the HTML form user submits the birth date, calculates this user's age

$year = 1992;

$month = 11;

$day = 2;

$birthday =mktime (0,0,0, $month, $day, $year); Turn the date of birth into a Unix timestamp

$nowdate =time ();

$ageunix = $nowdate-$birthday;

$age =floor ($ageunix/60/60/24/365);

echo "Your age is". $age. " Years ";

?>

Second, get the date and time in PHP

Third, modify the default time zone of PHP

Iv. Calculation of PHP script execution time using microseconds

<?php

Calculate PHP script execution time using microseconds

Class timer{

Private $startTime = 0;

Private $stopTime = 0;


function Start () {

$this->starttime=microtime (TRUE);

}

function Stop () {

$this->stoptime=microtime (TRUE);

}

function spent () {

Return round (($this->stoptime-$this->starttime), 4);

}

}


$Timer =new Timer ();


$Timer->start ();

Usleep (2000);

$Timer->stop ();


echo "When executing this script". $Timer->spent (). " Seconds ";

?>

Five, Calendar class

This article from the "12145704" blog, reproduced please contact the author!

Date and time of PHP

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.