Date_format date format in Smarty

Source: Internet
Author: User
Tags current time php file smarty template

Formatted output dates and times in PHP are available: Date (' y-m-d h:i:s ', timestamp); The form of output, corresponding to the "year-month-day: minutes: seconds."


which formats are supported by the Date_format function in Smarty

Date_format Support Format:

%a-shorthand for the current area of the week

%A-The full name of the current area week

%b-shorthand for the current area month

%B-Full name of the current region month

%c-The preferred datetime expression for the current region

%c-century value (year divided by 100 and rounded, range from 00 to 99)

%d-days of the month, decimal digits (range from 01 to 31)

%d-Same as%m/%d/%y

%e-Day of the month, decimal digits, one digit preceded by a space (range from ' 1 ' to ' 31 ')

%g-like%g, but no century.

Year of%g-4 number of digits

%h-like%b.

%H-24-Hour decimal hours (range from 00 to 23)

%I-12-Hour decimal hours (range from 00 to 12)

%j-Day of the Year, decimal number (range from 001 to 366)

%k-Hours, 24-hour format, no leading zeros

%l-Hours, 12-hour format, no leading zeros

%m-Decimal month (range from 01 to 12)

%m-Decimal minutes

%n-line Feed

%p-based on a given time value of ' am ' or ' PM ', or the corresponding string in the current locale

%r-time with a.m. and p.m. Symbols

Time of%r-24 hour symbol

%s-Decimal seconds

%t-Tab

%T-current time, same as%h:%m:%s

%u-decimal representation of the day of the week [1,7],1 Monday

%u-the first week of the first week of the year, starting on the first Sunday

%V-The ISO 8,601:1988 format for the first weeks of the year, ranging from 01 to 53, and the 1th week of the year at least 4 days of the week, Monday as the first day of the week. (Use%G or%G as the year that corresponds to the number of weeks for the specified timestamp.) )

%w-Day of the week, Sunday to 0

%w-Number of weeks of the year, starting from the first Monday of the first week as the first day

%x-Preferred time notation for the current region, excluding time

%x-Preferred time notation for the current region, excluding dates

%y-Decimal year without century (range from 00 to 99)

%Y-including the decimal year of the Century

%Z-time zone name or abbreviation

Percent%-the '% ' character on the text.


And in the Smarty template, such as $time is a PHP file in the assign over the timestamp, in the template file written as:

<{$time |date_format: '%y-%m-%d%h:%m:%s '}> , the corresponding output format is: "Year-month-day: minutes: seconds."

The code is as follows Copy Code


<?php

Import custom Smarty Action class smartyinit.php
Include_once (' class/smartyinit.php ');
$smarty = new Smartyinit ();

Set the default time zone for Shanghai
Date_default_timezone_set (' Asia/shanghai ');
Output echo strtotime (' Now '), resulting in: 1245763672
Strtotime (' Now ') returns a timestamp

But also the timestamp from the database.
$time = time ();

  echo ' PHP formatted output: <br/> ';
  echo yesterday: '. Date (' y-m-d h:i:s ', Strtotime ('-1 day ')). ' <br/> ';
 //date (' y-m-d h:i:s '), without writing the second argument, tacitly considers the current time
 //can also be written as: Date (' y-m-d h:i:s ', Strtotime (' Now '))
  Echo ' Today: '. Date (' y-m-d h:i:s '). ' <br/> ';
  Echo ' Tomorrow: '. Date (' y-m-d h:i:s ', Strtotime (' 1 day ')). ' <br/> ';
  Echo ' assignment timestamp: '. Date (' y-m-d h:i:s ', $time). ' <br/> ';

Strtotime (' Today ') only outputs today's date,
Strtotime (' Today 00:00:00 ') can output time
$smarty->assign (' Yesterday ', Strtotime (' Yesterday '));
$smarty->assign (' Today ', Strtotime (' 20:15:04 '));
$smarty->assign (' Tomorrow ', Strtotime (' Tomorrow '));


$smarty->assign (' Yesterday1 ', Strtotime ('-1 day '));
Equivalent to $smarty->assign (' Today1 ', Strtotime (' 0 day '));
$smarty->assign (' Today1 ', strtotime (' Now '));
$smarty->assign (' Tomorrow1 ', Strtotime (' 1 day '));
$smarty->assign (' time ', $time);

$smarty->display (' index.html ');

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>smarty Test </title>

<body>

<p>smarty template output: <br/>
Yesterday: <{$yesterday |date_format: '%y-%m-%d%h:%m:%s '}>
<br/>
Today: <{$today |date_format: '%y-%m-%d%h:%m:%s '}>
<br/>
Tomorrow: <{$tomorrow |date_format: '%y-%m-%d%h:%m:%s '}>
</p>
<p>
Yesterday: <{$yesterday 1|date_format: '%y-%m-%d%h:%m:%s '}>
<br/>
Today: <{$today 1|date_format: '%y-%m-%d%h:%m:%s '}>
<br/>
Tomorrow: <{$tomorrow 1|date_format: '%y-%m-%d%h:%m:%s '}>
<br/>
Assignment timestamp: <{$time |date_format: '%y-%m-%d%h:%m:%s '}>
</p>

Smarty preserves variable output: <{$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"}>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.