php strtotime 将日间日期转换成时间时间戳

 更新时间:2016年11月25日 15:58  点击:1848

/*
 将日间日期转换成时间时间戳
 strtotime(time,now)参数 描述
 time 规定要解析的时间字符串。
 now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
 
 <?php
 echo strtotime("now"), " ";
 echo strtotime("10 September 2000"), " ";
 echo strtotime("+1 day"), " ";
 echo strtotime("+1 week"), " ";
 echo strtotime("+1 week 2 days 4 hours 2 seconds"), " ";
 echo strtotime("next Thursday"), " ";
 echo strtotime("last Monday"), " ";
 ?>
 
 <?php
 $str = 'Not Good';
 
 // previous to PHP 5.1.0 you would compare with -1, instead of false
 if (($timestamp = strtotime($str)) === false) {
  echo "The string ($str) is bogus";
 } else {
  echo "$str == " . date('l dS o F Y h:i:s A', $timestamp);
 }
 ?>
 
 再看strtotime实例
*/
 echo strtotime('2010-2-14'),"<br />";
 echo date('Y-m-d',strtotime('2010-2-14'));
 
 //输出值
 
 1266076800
 2010-02-14
 
 //你应该在strtotime(),你决定什么不能做。例如
 <?php

# on 2/8/2010
date('m/d/y', strtotime('first day')); # 02/01/10
date('m/d/y', strtotime('last day')); # 02/28/10
date('m/d/y', strtotime('last day next month')); # 03/31/10
date('m/d/y', strtotime('last day last month')); # 01/31/10
date('m/d/y', strtotime('2009-12 last day')); # 12/31/09 - this doesn't work if you reverse the order of the year and month
date('m/d/y', strtotime('2009-03 last day')); # 03/31/09
date('m/d/y', strtotime('2009-03')); # 03/01/09
date('m/d/y', strtotime('last day of march 2009')); # 03/31/09
date('m/d/y', strtotime('last day of march')); # 03/31/10
?>

定义和用法
strftime() 函数根据区域设置格式化本地时间/日期。

语法
strftime(format,timestamp)参数 描述
format 可选。规定如何返回结果。
timestamp 可选。
提示和注释
提示:与 gmstrftime() 的行为相同,不同的是返回时间是本地时间。
例子
输出 strftime() 和 gmstrftime() 的结果:

<?php教程
echo(strftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));

//输出当前日期、时间和时区
echo(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time()));
?>输出:

Dec 31 1998 20:00:00
Dec 31 1998 19:00:00
It is Wed on Jan 25, 2006, 11:32:10 time zone: W. Europe Standard Time

format  Description Example returned values
Day --- ---
%a An abbreviated textual representation of the day Sun through Sat
%A A full textual representation of the day Sunday through Saturday
%d Two-digit day of the month (with leading zeros) 01 to 31
%e Day of the month, with a space preceding single digits 1 to 31
%j Day of the year, 3 digits with leading zeros 001 to 366
%u ISO-8601 numeric representation of the day of the week 1 (for Monday) though 7 (for Sunday)
%w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
Week --- ---
%U Week number of the given year, starting with the first Sunday as the first week 13 (for the 13th full week of the year)
%V ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with

Monday being the start of the week 01 through 53 (where 53 accounts for an overlapping week)
%W A numeric representation of the week of the year, starting with the first Monday as the first week 46 (for the 46th week

of the year beginning with a Monday)
Month --- ---
%b Abbreviated month name, based on the locale Jan through Dec
%B Full month name, based on the locale January through December
%h Abbreviated month name, based on the locale (an alias of %b) Jan through Dec
%m Two digit representation of the month 01 (for January) through 12 (for December)
Year --- ---
%C Two digit representation of the century (year divided by 100, truncated to an integer) 19 for the 20th Century
%g Two digit representation of the year going by ISO-8601:1988 standards (see %V) Example: 09 for the week of January 6, 2009
%G The full four-digit version of %g Example: 2008 for the week of January 3, 2009
%y Two digit representation of the year Example: 09 for 2009, 79 for 1979
%Y Four digit representation for the year Example: 2038
Time --- ---
%H Two digit representation of the hour in 24-hour format 00 through 23
%I Two digit representation of the hour in 12-hour format 01 through 12
%l (lower-case 'L') Hour in 12-hour format, with a space preceeding single digits 1 through 12
%M Two digit representation of the minute 00 through 59
%p UPPER-CASE 'AM' or 'PM' based on the given time Example: AM for 00:31, PM for 22:23
%P lower-case 'am' or 'pm' based on the given time Example: am for 00:31, pm for 22:23
%r Same as "%I:%M:%S %p" Example: 09:34:17 PM for 21:34:17
%R Same as "%H:%M" Example: 00:35 for 12:35 AM, 16:44 for 4:44 PM
%S Two digit representation of the second 00 through 59
%T Same as "%H:%M:%S" Example: 21:34:17 for 09:34:17 PM
%X Preferred time representation based on locale, without the date Example: 03:59:16 or 15:59:16
%z Either the time zone offset from UTC or the abbreviation (depends on operating system) Example: -0500 or EST for Eastern

Time
%Z The time zone offset/abbreviation option NOT given by %z (depends on operating system) Example: -0500 or EST for Eastern

Time
Time and Date Stamps教程 --- ---
%c Preferred date and time stamp based on local Example: Tue Feb 5 00:45:10 2009 for February 4, 2009 at 12:45:10 AM
%D Same as "%m/%d/%y" Example: 02/05/09 for February 5, 2009
%F Same as "%Y-%m-%d" (commonly used in database datestamps) Example: 2009-02-05 for February 5, 2009
%s Unix Epoch Time timestamp (same as the time() function) Example: 305815200 for September 10, 1979 08:40:00 AM
%x Preferred date representation based on locale, without the time Example: 02/05/09 for February 5, 2009
Miscellaneous --- ---
%n A newline character (" ") ---
%t A Tab character (" ") ---
%% A literal percentage character ("%") ---


<?php
/*     December 2002 / January 2003
ISOWk  M   Tu  W   Thu F   Sa  Su
----- ----------------------------
51     16  17  18  19  20  21  22
52     23  24  25  26  27  28  29
1      30  31   1   2   3   4   5
2       6   7   8   9  10  11  12
3      13  14  15  16  17  18  19   */

// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
echo "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/28/2002")) . " ";

// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
echo "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/30/2002")) . " ";

// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
echo "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . " ";

// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
echo "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . " ";

 

/*     December 2004 / January 2005
ISOWk  M   Tu  W   Thu F   Sa  Su
----- ----------------------------
51     13  14  15  16  17  18  19
52     20  21  22  23  24  25  26
53     27  28  29  30  31   1   2
1       3   4   5   6   7   8   9
2      10  11  12  13  14  15  16   */

// Outputs: 12/23/2004 - %V,%G,%Y = 52,2004,2004
echo "12/23/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/23/2004")) . " ";

// Outputs: 12/31/2004 - %V,%G,%Y = 53,2004,2004
echo "12/31/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/31/2004")) . " ";

// Outputs: 1/2/2005 - %V,%G,%Y = 53,2004,2005
echo "1/2/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/2/2005")) . " ";

// Outputs: 1/3/2005 - %V,%G,%Y = 1,2005,2005
echo "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . " ";

?>
]
获取指定日期的unix时间戳 strtotime("2009-1-22") 示例如下:
echo strtotime("2009-1-22")  结果:1232553600
说明:返回2009年1月22日0点0分0秒时间戳

二,获取英文文本日期时间 示例如下:
便于比较,使用date将当时间戳与指定时间戳转换成系统时间

(1)打印明天此时的时间戳strtotime("+1 day")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("+1 day")) 结果:2009-01-23 09:40:25

(2)打印昨天此时的时间戳strtotime("-1 day")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("-1 day")) 结果:2009-01-21 09:40:25

(3)打印下个星期此时的时间戳strtotime("+1 week")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("+1 week")) 结果:2009-01-29 09:40:25

(4)打印上个星期此时的时间戳strtotime("-1 week")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("-1 week")) 结果:2009-01-15 09:40:25

(5)打印指定下星期几的时间戳strtotime("next Thursday")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("next Thursday")) 结果:2009-01-29 00:00:00

(6)打印指定上星期几的时间戳strtotime("last Thursday")
当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25
指定时间:echo date("Y-m-d H:i:s",strtotime("last Thursday")) 结果:2009-01-15 00:00:00

function testAddslashes($array) {
 if(!get_magic_quotes_gpc()) {
  if(is_array($array)) {
   foreach($array as $key => $val) {
    $array[$key] = testAddslashes($val);
   }
  } else {   
   $array = addslashes($array);
  }
  $array=str_replace("&#x","& # x",$array); //过滤一些不安全

字符s
  $array=str_replace("<","&lt;",$array); //过滤<
 }
 return $array;
}

if( $_POST)
{
 print_r( $_POST );
 echo '过滤前<hr /> ';
 $_POST = testAddslashes($_POST);
 echo '<hr />过滤后<br />';
 echo $_POST['textfield'];
 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>

<form action="" method="post" enctype="multipart/form-data" name="form1"

id="form1">
  <label>
  <input type="text" name="textfield" />
  </label>
  <p>
    <label>
    <input type="text" name="textfield2" />
    </label>
    <label></label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
  </p>
</form>
</body>
</html>

//用php curl_init函数
$ch = curl_init();
  $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, "$url");
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0

(compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $contents = curl_exec($ch);
  curl_close($ch);
  
//方法用php fsockopen

function ReadPR($link)
{
 $fp = fsockopen ("www.google.com", 80, $errno, $errstr, 30);
 if (!$fp)
 {
  echo "$errstr ($errno) ";
  exit(1);
 }
 else
 {
  $out = "GET $link HTTP/1.0 ";
  $out .= "Host: toolbarqueries.google.com ";
  $out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar

2.0.114.9-big; Linux 2.6) ";
  $out .= "Connection: Close ";
  fwrite($fp, $out);
  do{
  $line = fgets($fp, 128);
  }while ($line !== " ");
  $data = fread($fp,8192);
  fclose ($fp);
  return $data;
 }
}

$now_time = time(); //当前时间
$for_time = mktime(0,0,0,2,24,2010);//设置一个日期
$n_time = $now_time-$for_time;
if( $n_time > 2592000 )

 $echo = round($n_time/2592000).'月前';
}
elseif( $n_time > 6048000 )
{
 $echo = round($n_time/604800).'周前';
}
elseif( $n_time > 86400 )
{
 $echo = round($n_time/86400).'天前';
}
elseif( $n_time > 3600 )
{
 $echo = round($n_time/3600).'小时前';
}
elseif( $n_time > 60 )
{
 $echo = round($n_time/60).'分钟前';
}
else
{
 $echo = $n_time.'秒前';
}
echo $echo;

[!--infotagslink--]

相关文章

  • PostgreSQL TIMESTAMP类型 时间戳操作

    这篇文章主要介绍了PostgreSQL TIMESTAMP类型 时间戳操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-26
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • 解决python 两个时间戳相减出现结果错误的问题

    这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
  • php计算两个日期相差天数的方法

    本文实例讲述了php计算两个日期相差天数的方法。...2015-03-15
  • PostgreSQL 字符串处理与日期处理操作

    这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
  • vue开发之moment的介绍与使用

    moment是一款多语言支持的日期处理类库, 在vue中如何使用呢?这篇文章主要给大家介绍了关于vue之moment使用的相关资料,需要的朋友可以参考下...2021-05-13
  • 非常全面的php日期时间运算汇总

    实例讲解之前,先来介绍几个核心函数: mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。...2015-11-08
  • C#实现将字符串转换成日期格式的方法

    这篇文章主要介绍了C#实现将字符串转换成日期格式的方法,涉及C#操作时间及字符串的相关技巧,非常简单实用,需要的朋友可以参考下...2020-06-25
  • php根据日期或时间戳获取星座信息和生肖等信息

    分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birthext($birth){ if(strstr($birth,'-')===false&&strlen($bi...2015-10-21
  • php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法

    php获取今日开始时间戳和结束时间戳$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;//php获取昨日起始时间戳和结束时间...2013-10-04
  • Unity时间戳的使用方法

    这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • java8时间 yyyyMMddHHmmss格式转为日期的代码

    这篇文章主要介绍了java8时间 yyyyMMddHHmmss格式转为日期的代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • C#判断日期是否到期的方法

    这篇文章主要介绍了C#判断日期是否到期的方法,是C#程序设计中非常实用的技巧,需要的朋友可以参考下...2020-06-25
  • C#比较日期的方法总结

    在本篇内容中小编给大家整理了关于C#比较日期的方法和相关知识点,有需要的朋友们学习下。...2020-06-25
  • 微信小程序wxs日期时间处理的实现示例

    最近在做一个列表的时候,涉及到时间格式化操作。本文主要介绍了微信小程序wxs日期时间处理的实现示例,分享给大家,感兴趣的可以了解一下...2021-07-22
  • sql通过日期判断年龄函数的示例代码

    这篇文章主要介绍了sql通过日期判断年龄函数,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-07-16
  • jQuery DateTimePicker 日期和时间插件示例

    jQuery UI很强大,其中的日期选择插件Datepicker是一个配置灵活的插件,这篇文章主要介绍了jQuery DateTimePicker 日期和时间插件示例,有兴趣的可以了解一下。...2017-01-26
  • C语言实现时间戳转日期的算法(推荐)

    下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
  • 微信小程序实现根据日期和时间排序功能

    这篇文章主要为大家详细介绍了微信小程序实现根据日期和时间排序功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-26
  • C# 设置系统日期格式的方法

    公司电脑各式各样的都有,里面的设置也有很多不统一的,我们做软件一般会从系统中获取一些数据,比如日期时间,环境变量的路径参数,可以用批处理文件达到我们所想要的目的,也可以用C#代码...2020-06-25