Date and Time
The PHP date() function is used to format date and/or a time and formats as timestamp to a more readable data and time.
date(format, timestamp) //format is required and timestamp is optional
Here are some characters that are commonly uesd for dates:
- d -- represents the day of the month
- m -- represents a month
- Y -- represents a year
- l -- represents the day of the week
Other characters like '/','.' or '-' can also be inserted between the characters to add additional formatting.
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
output:
Today is 2016/02/17
Today is 2016.02.17
Today is 2016-02-17
Today is Wednesday
Use the date() function to automatically update the copyright year on the website
© 2010-<?php echo date("Y"); ?>
Here are some characters that are commonly used for times:
- h -- 12-hour format of an hour with leading zeros
- i -- minites with leading zero
- s -- seconds with leading zeros
- a -- lowecase ante meridiem and post meridiem
<!DOCTYPE html>
<html>
<body>
<?php
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
output:The time is 11:22:38pm
PHP use date_default_timezone_set() function to set timezone
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
output:The time is 11:24:21pm
The mktime() function returns the Unix timestamp for a date.The Unix timestamp contais the number of seconds between the Unix Epoch and the time specified.
mktime(hour, minute, second, month, day, year)
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
output:Created date is 2014-08-12 11:14:54am
The PHP strtotime() function is used to convert a human readable string to a Unix time.
strtotime(time, now)
<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
output:Created date is 2014-04-15 10:30:00pm
<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>
<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks",$startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate),"<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>
Date and Time的更多相关文章
- JavaScript Date对象
本篇主要介绍 Date 日期和时间对象的操作. 目录 1. 介绍:阐述 Date 对象. 2. 构造函数:介绍 Date 对象的构造函数new Date()几种方式. 3. 实例方法:介绍 Date ...
- ExtJS 4.2 Date组件扩展:添加清除按钮
ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...
- Java 时间类-Calendar、Date、LocalDate/LocalTime
1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...
- 为什么你SQL Server的数据库文件的Date modified没有变化呢?
在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...
- mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!
mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法! 修改mysql5.7的配置文件即可解决,方法如下: linux版:找到mysql的安装路径进入默认的为/usr/shar ...
- date命令
GNU的date提供+%s(小写s), 能打印出自1970-01-01 00:00:00到当前时间的秒数. 这可能大家都不陌生,但有两点需要注意: 1. %s存在于GNU扩展版本.像在solaris等 ...
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
- Date.parse
JavaScript: Date.parse(),一个参数,参数类型是 JavaScript 中的 Date 类型. 返回值 : 得到一个 Unix 时间戳,比如说,1470993235000,这种东 ...
- Mysql FROM_UNIXTIME效率 VS PHP date()效率 数据说话!
这几天在做数据统计,有几个统计图的需求是这样的: 按照年.月.日统计订单数量, 比方一年12个月,统计出1月多少订单,二月多少订单,按照这种模式统计. 但是数据库里存放的是 timestamp 的 ...
- 扩展JS Date对象时间格式化功能
在自己JS代码中引入一下代码: Date.prototype.format =function(format) { var o = { "M+" : this.getMonth() ...
随机推荐
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- jdbc 配置properties实现
package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...
- Struts、JSTL标签库的基本使用方法
一 使用Struts标签之前需要经过下面3个步骤的配置. 1.导入TLD文件. 2.在web.xml中注册标签库. 3.在页面中引入标签库. 下面详细介绍以上步骤. 1 导入TLD文件. TLD文件是 ...
- 支持向量机的smo算法(MATLAB code)
建立smo.m % function [alpha,bias] = smo(X, y, C, tol) function model = smo(X, y, C, tol) % SMO: SMO al ...
- Mysql 修改列的顺序
alter table 表名 modify 字段名 字段类型 after 字段举例alter table user_info modify user_name varchar(10) after us ...
- 深入理解Redis:底层数据结构
简介 redis[1]是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 文本信息“welcome to java programming!”
import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...
- Zabbix源码包安装
Zabbix源码包安装 Cenos5.3 Basic server 安装顺序 Libxml2 Libmcrypt Zlib Libpng Jpeg:需要创建目录jpeg /bin /lib / ...
- 使用rosed编辑ROS文件
1.1使用rosed. rosed是rosbash套件的一部分.它可以使你通过package的名字直接编辑一个package中的文件而不用输入package的整个路径. 用法: $ rosed [pa ...
- function format_number(srcNumber, n) {
function format_number(srcNumber, n) {var dstNumber = parseFloat(srcNumber);if (isNaN(dstNumber)) {r ...