Hướng dẫn strtotime trong php w3schools

❮ PHP Date/Time Reference

Example

Parse English textual datetimes into Unix timestamps:

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("3 October 2005") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday"));
?>

Try it Yourself »


Definition and Usage

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.


Syntax

Parameter Values

ParameterDescription
time Required. Specifies a date/time string
now Optional. Specifies the timestamp used as a base for the calculation of relative dates


Technical Details

Return Value:Returns a timestamp on success. FALSE on failure
PHP Version:4+
PHP Changelog:PHP 5.3.0: Relative time formats such as this week, previous week, last week, and next week now interprets a week period of Monday through Sunday, rather then a 7-day period relative to the current date/time
PHP 5.3.0: Now 24:00 is a valid format
PHP 5.2.7: In earlier versions, if requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month it would incorrectly add one week to the returned timestamp. This has been corrected now
PHP 5.1.0: Returns FALSE on failure (earlier versions returns -1), and issues E_STRICT and E_NOTICE time zone errors
PHP 5.0.2: Now correctly computes "now" and other relative times from current time, not from today's midnight
PHP 5.0.0: Allows microseconds (but they are ignored)

❮ PHP Date/Time Reference


Learn PHP

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

Start learning PHP now »


Easy Learning with "PHP Tryit"

With our online "PHP Tryit" editor, you can edit the PHP code, and click on a button to view the result.

Example

<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>

Try it Yourself »

Click on the "Try it Yourself" button to see how it works.


PHP Exercises



PHP Examples

Learn by examples! This tutorial supplements all explanations with clarifying examples.

See All PHP Examples


PHP Quiz Test

Learn by taking a quiz! This quiz will give you a signal of how much you know, or do not know, about PHP.

Start PHP Quiz!


My Learning

Track your progress with the free "My Learning" program here at W3Schools.

Log into your account, and start earning points!

This is an optional feature, you can study W3Schools without using My Learning.

Hướng dẫn strtotime trong php w3schools


PHP References

W3Schools' PHP reference contains different categories of all PHP functions, keywords and constants, along with examples.


Kickstart your career

Get certified by completing the course

Get certified

w3schoolsCERTIFIED.2022



Hàm date() sẽ xuất ra một chuỗi thời gian dựa theo định dạng được truyền vào và số nguyên timestamp.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: date( $format,  $timestamp);

Trong đó:

  • $format là định dạng  thời gian mà hàm sẽ trả về.
  • $timestamp là một số nguyên Unix timestamp. nếu không truyền tham số này, nó sẽ lấy timestamp của thời điểm hiện tại.

Kết quả trả về

Hàm sẽ trả về một chuỗi thời gian đúng với định dạng $format đã truyền vào nếu chạy thành công. Nếu $timestamp truyền vào không phải là số hàm sẽ trả về FALSE và sẽ xuất ra cảnh báo.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Cách sử dụng hàm $date() lấy thời gian hiện tại.

Code

echo date("F j, Y, g:i a")."<br />";                 
echo date("m.d.y")."<br />";                         
echo date("j, n, Y")."<br />";                      
echo date("Ymd")."<br />";                           
echo date('h-i-s, j-m-y, it is w Day')."<br />";     
echo date('\i\t \i\s \t\h\e jS \d\a\y.')."<br />";   
echo date("D M j G:i:s T Y")."<br />";              
echo date('H:m:s \m \i\s\ \m\o\n\t\h')."<br />";     
echo date("H:i:s")."<br />";                         
echo date("Y-m-d H:i:s")."<br />";  

Kết quả

May 4, 2017, 2:22 pm
05.04.17
4, 5, 2017
20170504
02-22-35, 4-05-17, 2231 2235 4 Thupm17
it is the 4th day.
Thu May 4 14:22:35 ICT 2017
14:05:35 m is month
14:22:35
2017-05-04 14:22:35

Định dạng thời gian tại một thời điểm nào đó.

Code

$date = mktime(15,50,30,4,30,2017);
echo date("F j, Y, H:i a", $date)."<br />";                 
   
echo date("Y-m-d H:i:s", $date)."<br />";  

Kết quả

April 30, 2017, 15:50 pm
2017-04-30 15:50:30

Tham khảo: php.net

Một phần kiến thức trong lập trình PHP căn bản đó là xử lý thời gian, ngày, tháng với hàm Date() và Time(). Bài viết sau sẽ hướng dẫn bạn cách sử dụng Date và Time một cách dễ dàng.

  • Hàm Date() trong PHP và cách sử dụng
  • Hàm Time() trong PHP

Hàm Date() trong PHP và cách sử dụng

Cú pháp:

date( $format, $timestamp);

$format: định dạng biến trả vể

$timestamp: là số nguyên Unix timestamp. nếu để trống nó sẽ lấy về thời gian hiện tại.

Các kiểu Format:

h: Trả về giờ trong ngày kiểu 12h
H: Trả về giờ trong ngày kiểu 24h
i: Trả về phút trong giờ
s: Trả về số giây trong phút
d: trả về ngày trong tháng(từ 1- đến31)
j: Trả về ngày trong tháng, nhưng nếu ngày<10 sẽ không hiển thị số 0
D: Trả về định dạng thứ trogn tuần bằng tiếng anh viết tắt
l: Trả về đầy đủ thứ trong tuần (tiếng anh)
m: Trả về tháng trong năm (từ 1 đến 12)
M: Trả về tháng trong năm nhưng bằng tiếng anh viết tắt
y: Trả về 2 số cuối cùng của năm
Y: trả về đầy đủ 4 số của năm

Ví dụ:

<?php
echo date('H:i:s d-m-Y');
?>

Khi bạn chạy đoạn code trên nó sẽ xuất ra giờ, phút, giây và ngày, tháng, năm.

Các kiểu viết giờ, tháng, năm khác:

<?php
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>

Hàm time() PHP sẽ lấy thời gian về thời điểm hiện tại. Nó sẽ trả về số giây tính từ thời điểm 00:00:00 1/1/1970.

Cú pháp:

time()

Ví dụ:

<?php
// 7 ngày; 24 giờ; 60 phút; 60 giây
$nextWeek = time() + (7 * 24 * 60 * 60);

echo 'Hôm nay: '. date('Y-m-d') ."<br>";
echo 'Tuần tới: '. date('Y-m-d', $nextWeek) ."<br>";
?>

Kết thúc bài học này bạn đã hiểu 2 hàm date() và time() để lấy lấy ngày giờ hiện tại trong PHP chưa nào? Trong những bài tiếp theo mình sẽ tiếp tục chia sẻ những kiến thức PHP hay nhất mời bạn cùng tiếp tục theo dõi tại Quach Quynh blog nhé!