PHP Date Conversion int type

Source: Internet
Author: User
Tags echo date
$a = "2001-5-2";
$date = Explode ('-', $a);
Explode splits the string by '-' to put the split data into the array, which is the part of the
$date [0] = 1997, $date [1] = 3, $date [2] = 2
$timestamp = Mktime (0,0,0, $date [1], $date [2], $date [0]);


echo $timestamp + (3600*8). " <br> ";//Time difference 8 hours calculation
echo Date (' y-m-d h:i:s ', $timestamp). ' <br> ';

echo Date (' y-m-d h:i:s ', $timestamp). ' <br> '; H:i:s 12 hours

echo Date (' y/m/d h:i:s ', $timestamp + (3600*24)); Date Calculation
Example 1. Time () example

[color=green]<?php
$nextWeek = time () + (7 * 24 * 60 * 60);
7 days; Hours; mins; 60secs
Echo ' Now: '. Date (' y-m-d '). " \ n ";
Echo ' Next Week: '. Date (' y-m-d ', $nextWeek). " \ n ";
?>

The output of the previous example is similar to the following:

Now:2005-03-30
Next week:2005-04-07



PHP Date Conversion
MySQL in the database time format, the most common two storage methods, one is the MySQL preset datetime, the format will be like 2009-11-03 20:10:43, the other is to save into the UNIX times format, can be set to int (11), Both of these can actually be used, in the phpBB2 is adopted in the latter way, because open source to support a variety of databases, but also unified code, so crisp with the Unix time tag, so it is better to convert the time zone, if you use the UNIX format, you can take advantage of the Times () function to fetch.

#
# Get current system UNIX time
echo Time ();
# Next week time
$nextWeek = time () + (7 * 24 * 60 * 60);
# 7 days; Hours; mins; 60secs
# Another can use Mktime to get system UNIX time
# UNIX time for today's date
Echo mktime (0,0,0, Date ("Y"), Date ("M"), Date ("D"));
So we can store it in MySQL database, use time () to INSERT, and then how to show the Date: Date () function

# using the date () function
$time = time ();
echo Date ("Y-m-d h:i:s", $time);
$nextWeek = time () + (7 * 24 * 60 * 60);
echo Date ("Y-m-d h:i:s", $nextWeek);
If the database format is datetime, then the value taken out must be y-m-d h:i:s format, then how to convert to UNIX time, you can use Strtotime

#
# put the standard time into the first parameter
Echo strtotime ("2009-10-10 20:22:10");
Echo Strtotime ("Now");
Conclusion is
1. If the database is using INT (11), use Date (), Mktime (), Time () to convert to the temporal format
2. If the database is datetime, use Strtotime () to convert to UNIX time for date addition and subtraction.
  • 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.