首页 > 编程技术 > php

返回当前时间的 Unix 时间戳

发布时间:2016-11-25 15:12

PHP time() 函数
PHP Date / Time 函数
定义和用法
time() 函数返回当前时间的 Unix 时间戳。

语法
time(void)参数 描述
void 可选。

说明
返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
提示和注释
提示:自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。
例子
例子 1
<?php教程
$t=time();
echo($t . "<br />");
echo(date("D F d Y",$t));
?>输出:

1138618081
Mon January 30 2006例子 2
<?php
$nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ." ";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ." ";
?>输出:

Now:       2005-03-30
Next Week: 2005-04-07

返回默认时区 设置默认时区

PHP date_default_timezone_get() 函数
PHP Date / Time 函数
定义和用法
date_default_timezone_get() 函数返回脚本中所有日期时间函数所使用的默认时区。

语法
date_default_timezone_get(void)参数 描述
void 可选。

说明
本函数返回默认时区,使用如下“假定”的顺序:

用 date_default_timezone_set() 函数设定的时区(如果设定了的话)
TZ 环境变量(如果非空)
date.timezone 配置选项(如果设定了的话)
自己推测(如果操作系统支持)
如果以上选择都不成功,则返回 UTC
例子
<?php教程
echo(date_default_timezone_get());
?>输出:

Europe/Paris


PHP date_default_timezone_set() 函数
PHP Date / Time 函数
定义和用法
date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。

语法
date_default_timezone_set(timezone)参数 描述
timezone 必需。时区标识符,比如 "UTC" 或 "Europe/Paris"。

 

说明
注释:自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息,如果使用系统设定或 TZ 环境变量则还会产生 E_STRICT 级别的信息。
例子
<?php
echo(date_default_timezone_set("Europe/Paris"));
?>输出:

1

定义和用法
strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

语法
strtotime(time,now)参数 描述
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。 

说明
该函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数,则用系统当前时间。

该函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。
此过程在 date_default_timezone_get() 函数页面中有说明。


PHP date_default_timezone_get() 函数
PHP Date / Time 函数
定义和用法
date_default_timezone_get() 函数返回脚本中所有日期时间函数所使用的默认时区。

语法
date_default_timezone_get(void)参数 描述
void 可选。

说明
本函数返回默认时区,使用如下“假定”的顺序:

用 date_default_timezone_set() 函数设定的时区(如果设定了的话)
TZ 环境变量(如果非空)
date.timezone 配置选项(如果设定了的话)
自己推测(如果操作系统支持)
如果以上选择都不成功,则返回 UTC
例子
<?php教程
echo(date_default_timezone_get());
?>输出:

Europe/Paris


返回值
成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。
例子
<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>输出:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

对于使用for循环语句时,你知道有多少次你想执行一个语句或语句的列表。为此,

For循环称为一个明确的循环。在循环的语法是有点复杂,但对循环往往比在循环方便

。 for循环的语法如下:

for (initialization; condition; increment)
{
   code to be executed;
}
for语句时3括号内的半分开,冒号表达式。当for循环执行时,发生以下情况:

初始化表达式的执行。这个表达式通常初始化一个或多个循环计数器,但语法允许任

何复杂程度的表达。
条件表达式求值。如果条件值为true,则循环语句。如果条件的值为false,为循环终

止。
此更新的表达增量执行。
该语句的执行,控制返回到步骤2。
有一个很简单的例子,打印出从0到10个号码:

<?php教程
for ($i=0; $i <= 10; $i++)
{
   echo "The number is ".$i."<br />";
}
?>

下一个例子生成到9乘法表2。外循环负责产生的股息列表,内环将负责生成每个数分

频器名单:

<?php
echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=50%";

for ($i = 1; $i <= 9; $i++ ) {   //this is the outer loop
  echo "<tr>";
  echo "<td>".$i."</td>";
 
   for ( $j = 2; $j <= 9; $j++ ) { // inner loop
        echo "<td>".$i * $j."</td>";
    }

   echo "</tr>";
}

echo "</table>";
?>

最后让我们来看看这个例子,它使用2变量。一至1添加到所有的号码为10。另一只加

入偶数

<?php
$total = 0;
$even = 0;

for ( $x = 1, $y = 1; $x <= 10; $x++, $y++ ) {
  if ( ( $y % 2 ) == 0 ) {
    $even = $even + $y;
  }
  $total = $total + $x;
}

echo "The total sum: ".$total."<br />";
echo "The sum of even values: ".$even;

?>

有时可以是有益的对当前数据库教程模式转储。下面的脚本读取MySQL数据库和输出的XML

描述模式架构。

首先,我们连接到MySQL数据库和使用SHOW TABLES命令返回所有数据库中的表。下一

步,我们遍历每个表和返回每个使用SHOW场命令表中的字段。最后,我们提出了到XML

返回的所有信息。

有一个看一看代码:


<?php
// database constants
// make sure the information is correct
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");
define("DB_NAME", "tutorials");

// connection to the database
$dbhandle = mysql教程_connect(DB_SERVER, DB_USER, DB_PASS)
   or die("Unable to connect to MySQL");

// select a database to work with
$selected = mysql_select_db(DB_NAME, $dbhandle)
   or die("Could not select examples");

// return all available tables
$result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle );

$tables = array();
while ($row = mysql_fetch_row($result_tbl)) {
   $tables[] = $row[0];
}

$output = "<?xml version="1.0" ?> ";
$output .= "<schema>";

// iterate over each table and return the fields for each table
foreach ( $tables as $table ) {
   $output .= "<table name="$table">";
   $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

   while( $row1 = mysql_fetch_row($result_fld) ) {
      $output .= "<field name="$row1[0]" type="$row1[1]"";
      $output .= ($row1[3] == "PRI") ? " primary_key="yes" />" : " />";
   }

   $output .= "</table>";
}

$output .= "</schema>";

// tell the browser what kind of file is come in
header("Content-type: text/xml");
// print out XML that describes the schema
echo $output;

// close the connection
mysql_close($dbhandle);
?>

另一方法


$document = new DOMDocument('1.0');
$schemaNode = $document->createElement("schema");
$document->appendChild($schemaNode);

foreach ( $tables as $table ) {
$tableNode .= $document->createElement("table");
$schemaNode->appendChild($tableNode);
$result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

while( $row1 = mysql_fetch_row($result_fld) ) {
$fieldNode = $document->createElement("field");
$tableNode->appendChild($fieldNode);

$fieldNode->setAttribute("name", $row1[0]);
$fieldNode->setAttribute("type", $row1[1]);
if ($row1[3] == "PRI")
$fieldNode->setAttribute("primary_key", "yes");
}
}

...

echo $document->saveXML();

===========================

php 输出word文档

在此方法中您需要格式化的HTML / PHP页面使用Word友好CSS和标头信息添加到您的

PHP脚本。请确保您不使用因为一切外部样式表应在相同的文件。

因此,用户将被提示下载文件。这个文件将不会被100%的“原始”的Word文档,但它

肯定会在MS Word中打开应用程序。你可以使用这个既用于Unix和Windows环境的方法

<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv="Content-Type" content="text/html;

charset=Windows-1252">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
?>

方法二

方法2 - 使用COM对象

请注意,在服务器运行下面必须有MS Word中所述的代码安装。 COM将只能在Windows

上工作。

Word文档保存到临时目录,然后送往通过readfile()函数来浏览器

// Create new COM object – word.application
$word = new COM("word.application");

// Hide MS Word application window
$word->Visible = 0;

//Create new document
$word->Documents->Add();

// Define page margins
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';

// Define font settings
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;

// Add text
$word->Selection->TypeText("TEXT!");

// Save document
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);

// Close and quit
$word->quit();
unset($word);

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

// Send file to browser
readfile($filename);
unlink($filename);

标签:[!--infotagslink--]

您可能感兴趣的文章: