Home > Article > Backend Development > php date conversion to int type
$a="2001-5-2";
$date = explode('-',$a);
// explode splits the string according to '-' and puts the split data into an array , that is, $date
// $date[0] = 1997, $date[1] = 3, $date[2] = 2
$timestamp = mktime(0,0,0,$date[1],$date [2],$date[0]);
echo $timestamp+(3600*8)."
";//Time difference calculation of 8 hours
//echo date('Y-m-d H:i:s' ,$timestamp).'
';
echo date('Y-m-d h:i:s',$timestamp).'
'; //h:i:s 12 hours
// echo date('Y/m/d H:i:s',$timestamp+(3600*24)); //Date calculation
Example 1. time() example
[color=Green]$ nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."n";
?>
The output of the above example is similar to:
Now: 2005-03-30
Next Week: 2005 -04-07
PHP Date Conversion
MySQL In terms of database time format, the two most common storage methods are one is MySQL default datetime, and the displayed format will be like 2009-11-03 20 :10:43, the other is to save in UNIX time format, which can be set to int(11). Both of these can actually be used. In phpBB2, the latter method is used because open source needs to support multiple databases. , but we need to unify the program code, so we simply use the UNIX time stamp, which is easier to convert the time zone. If you use the UNIX format, you can use the time() function to get it.
#
# Get the current system UNIX time
echo time();
# Next week’s time
$nextWeek = time() + (7 * 24 * 60 * 60);
# 7 days; 24 hours; 60 mins ; 60secs
# Another way to get the system UNIX time is to use mktime
# The UNIX time of today’s date
echo mktime(0,0,0, date("Y"),date("m"),date("d "));
So we can store it in the mysql database and use time() to INSERT. Next, how to display the time: date() function
# Use 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 uses datetime, the extracted value must be in Y-m-d H:i:s format. How to convert it to UNIX time? You can use strtotime
#
# to put the standard time into the first parameter
echo strtotime("2009-10-10 20:22:10");
echo strtotime("now");
The conclusion is
1. If the database uses int(11), use date(), mktime(), time() converts to time format
2. If the database uses datetime, use strtotime() to convert to UNIX time to add and subtract dates