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

&copy; 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的更多相关文章

  1. JavaScript Date对象

    本篇主要介绍 Date 日期和时间对象的操作. 目录 1. 介绍:阐述 Date 对象. 2. 构造函数:介绍 Date 对象的构造函数new Date()几种方式. 3. 实例方法:介绍 Date ...

  2. ExtJS 4.2 Date组件扩展:添加清除按钮

    ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...

  3. Java 时间类-Calendar、Date、LocalDate/LocalTime

    1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...

  4. 为什么你SQL Server的数据库文件的Date modified没有变化呢?

    在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...

  5. mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!

    mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法! 修改mysql5.7的配置文件即可解决,方法如下: linux版:找到mysql的安装路径进入默认的为/usr/shar ...

  6. date命令

    GNU的date提供+%s(小写s), 能打印出自1970-01-01 00:00:00到当前时间的秒数. 这可能大家都不陌生,但有两点需要注意: 1. %s存在于GNU扩展版本.像在solaris等 ...

  7. 【Spring】SpringMVC中浅析Date类型数据的传递

    在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...

  8. Date.parse

    JavaScript: Date.parse(),一个参数,参数类型是 JavaScript 中的 Date 类型. 返回值 : 得到一个 Unix 时间戳,比如说,1470993235000,这种东 ...

  9. Mysql FROM_UNIXTIME效率 VS PHP date()效率 数据说话!

    这几天在做数据统计,有几个统计图的需求是这样的: 按照年.月.日统计订单数量, 比方一年12个月,统计出1月多少订单,二月多少订单,按照这种模式统计. 但是数据库里存放的是 timestamp  的  ...

  10. 扩展JS Date对象时间格式化功能

    在自己JS代码中引入一下代码: Date.prototype.format =function(format) { var o = { "M+" : this.getMonth() ...

随机推荐

  1. JavaWeb基础: Tomcat

    Tomcat目录层次结构 Tomcat本质上是一个Java应用,要启动Tomcat服务需要安装JRE并配置JRE的Path,才能通过Tomcat_HOME/bin/startup脚本正常启动Tomca ...

  2. Java 基础知识点(必知必会其一)

    如何将字符串转换为数字? package Day_2; /** * @author Administrator * 功能: 如何将字符串转换为数字? */ public class StringToI ...

  3. UITabBarController加载之后不显示sub view controller

    原代码: fileprivate func createSubiewControllers() { let newsVC = NewsViewController() let newsItem = U ...

  4. ios基础篇(三)——UIButton的详细介绍

    按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容: 一.UIButton的定义 UIButton *button=[[UIButton buttonWithTy ...

  5. lmdb简介——结合MVCC的B+树嵌入式数据库

    lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...

  6. FileUpload上传与下载

    后台代码: public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protecte ...

  7. BZOJ1570 [JSOI2008]Blue Mary的旅行

    建分层图,每一层表示一天的情况 从S向第0层的1号点连边,每层的n向T连INF的边 枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案 由于路径长度不会超过n,因此tot个人走这 ...

  8. Java:数组

    数组 数组是一种引用数据类型(所以才会看到 new int[]),数组的长度初始化完成后是固定的.在内存中初始化数组后的空间就固定下来,即便数组中的内容被清空了,但在内存中占有的空间保留了下来,依然是 ...

  9. 在Excel中引用其他宏

    在excel的使用过程中,会用到一些自定义函数,可以使用宏轻松的实现这些功能,问题是必须使用“启用宏的excel”,这样用户每次打开时都要启用宏. 现用以按背景色计划为例,解决以上问题: 1.新建一个 ...

  10. sqlserver 2008 左补齐字符串

    SQLServer:right函数 语法 Right(string, length)   Right 函数的语法具有下面的命名参数:   部分 说明 string 必要参数.字符串表达式,从中最右边的 ...