PHP时间的两种类型解析
所以存到数据库也有两种形式了(真正不止,我的应用就两种),时间戳类型我是保存为字符串的,这个是比较方便的.
正常日期类型是保存为DATE型的.
这两个PHP时间要注意一下,我平时用两种,所以,前几天建的表,把时间类型存为DATE的,我还一直用时间戳保存,一直资料写不进表,调试了好久的才知错误之处,是类型不符合,不给写入库.
像上面的时间戳是比较方便,但,显示的时候,不至于把这个1228348800串给客户看吧,GOD NOWS!
所 以就用到了这两个的转换,先说一下如何取得当前的时间戳,$date1=time();这样就取得当前的时间戳了,要转回2008-12-4这种格式呢, 用到date()这个函数了,在PHP中date()函数比较常用,如取得当前日期,可以用$date2=date('Y-m-d');,关于里面参数的 意思,如不懂就查一下php的手册了.
好,言归正传,把1228348800转成2008-12-4格式代码如下:
$date3=date('Y-m-d H:i:s',"1228348800");
这样就OK了,如还想得到小时,分钟秒,只要把'Y-m-d'改一下就可以了,不过要注意一下,PHP时间好像有8个小时的误差.加上就OK了.
时间戳转正常日期有了,反之呢,把正常日期格式转为时间戳呢,请看如下代码:
$year=((int)substr("2008-12-04",0,4));//取得年份
$month=((int)substr("2008-12-04",5,2));//取得月份
$day=((int)substr("2008-12-04",8,2));//取得几号
echo mktime(0,0,0,$month,$day,$year);
这样就能把正常日期转成时间戳了,这里如果有时分秒也是同理,
注意:php5.1以上时间戳会与实际时间相差8小时,PHP时间的具体解决办法如下
1、最简单的方法就是不要用php5.1以上的版本--显然这是不可取的方法!!!
2、修改php.ini。打开php.ini查找date.timezone 去掉前面的分号= 后面加Asia/Shanghai,重启apache服务器即可--缺点就是如果程序放到别人的服务器上,不能修改php.ini,那就奈何不得了。
3、在程序中添加时间的初始化的语句的 即:“date_default_timezone_set("Asia/Shanghai"); ”这个可以由程序员任意设置,我的推荐。
时区标识符,大陆内地可用的PHP时间值是:PRC,Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为中国,重庆,上海,乌鲁木齐) ,Etc/GMT-8,Asia/Harbin
港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北)
还有新加坡:Asia/Singapore
澳大利亚:Australia/Sydney

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
