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. ADO.NET事务处理,初始回调函数,多张表的数据在同一个DataGridView中展示

    执行ADO.NET事务包含四个步骤,接下来以Transaction对象为例介绍. (1)调用SQLConnection对象的BeginTransaction()方法,创建一个SQLTransactio ...

  2. 开源牛人 zcbenz

    事情是这样的,微软推出了Visual Studio Code,我很好奇他怎么做跨平台的,所以就找找资料,在他的网站中是这么描述的: Architecturally, Visual Studio Cod ...

  3. 从java 转到 c# 知识点

    1. 重写的父类方法 必须是虚方法 用virtual 关键字修饰 而子类必须用 ovrride 关键字 java中不需要,, 2. namespace => packet using => ...

  4. BZOJ3058 四叶草魔杖

    Poetize11的T3 蒟蒻非常欢脱的写完了费用流,发现...边的cost竟然只算一次!!! 然后就跪了... Orz题解:"类型:Floyd传递闭包+最小生成树+状态压缩动态规划首先Fl ...

  5. CSS从今以后不用发愁

    Bootstrap 简洁.直观.强悍的前端开发框架,让web开发更迅速.简单. Bootstrap3中文文档 Bootstrap2中文文档 http://www.bootcss.com/

  6. jquery 添加方法 : $.fn.方法名 = function(参数a,b,c){

    $.fn.image_checked = function(self,status,img_body,csrf_token){             $(this).live('click', fu ...

  7. 判断IE8

    var browser=navigator.appName;    var panduan_hide='style="display:none;"';    if(browser= ...

  8. lazyload 分页加载

    http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/ echo $d['pic']; ?>" src=&qu ...

  9. js循环

    $('.xcarcoin_tb').each(function(i){ var aika='爱卡币';                if(data[i]==0){                }e ...

  10. TaskTracker启动过程源码级分析

    TaskTracker也是作为一个单独的JVM来运行的,其main函数就是TaskTracker的入口函数,当运行start-all.sh时,脚本就是通过SSH运行该函数来启动TaskTracker的 ...