-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ae92c2
commit ef5f3ca
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
Práce s datumy - Nette\Utils\DateTime | ||
************************************* | ||
|
||
.[perex] | ||
[Nette\Utils\DateTime |api:] je statická třída, která rozšiřuje [php:datetime] o další užitečné funkce pro práci s časem a datumy. | ||
|
||
Všechny příklady předpokládají vytvořený alias: | ||
|
||
/--php | ||
use Nette\Utils\DateTime; | ||
\-- | ||
|
||
Pojďme se podívat jak je to snadné. | ||
|
||
/--php | ||
$netteRelease = DateTime::from('2006-01-23 10:54:00'); // vytvoří DateTime z řetězce | ||
$netteRelease->modify('+10 years, 1 month'); // přičte 10 let a 1 měsíc | ||
echo $netteRelease; // 2016-02-23 10:54:00 | ||
\-- | ||
|
||
/--php | ||
$birthday = DateTime::createFromFormat('d.m.Y', '26.02.1994'); // vytvoří DateTime z vlastního formátu | ||
echo $birthday->getTimestamp(); // 762265863 | ||
\-- | ||
|
||
.[note] | ||
metoda modify je z rodičovské třídy [php:datetime]. | ||
|
||
from($time) | ||
---------- | ||
Vytvoří [Nette\Utils\DateTime |api:] objekt z řetězce, čísla nebo jiného rozšíření [php:datetime] a postará se o nastavení timezone. | ||
|
||
/--php | ||
DateTime::from(1138013640); // vytvoří DateTime z timestamp | ||
DateTime::from(42); // vytvoří DateTime z aktuálního času a přičte 42 sekund | ||
DateTime::from('1994-02-26 04:15:32'); // vytvoří DateTime z datumu i času | ||
DateTime::from('1994-02-26'); // vytvoří DateTime z datumu a zbytek doplní nulami | ||
\-- | ||
|
||
createFromFormat($format, $time, $timezone = NULL) | ||
-------------------------------------------------- | ||
Rozšiřuje metodu //from// o možnost určit vlastní formát a timezone. | ||
/--php | ||
DateTime::createFromFormat('d.m.Y', '26.02.1994'); // vytvoří DateTime z formátu s výchozí timezone | ||
DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // nebo s vlastní timezone | ||
DateTime::createFromFormat('j. n. Y H:i', '8. 9. 2042 16:36'); // jiný příklad | ||
\-- | ||
|
||
setTimestamp($timestamp) | ||
------------------------ | ||
/--php | ||
$created = new DateTime(); | ||
$created->setTimestamp(1138013640); | ||
\-- | ||
|
||
getTimestamp() | ||
-------------- | ||
/--php | ||
$publicDate = DateTime::from('2017-02-03'); | ||
$publicDate->getTimestamp(); //1486076400 | ||
\-- | ||
|
||
modifyClone($modify = '') | ||
------------------------- | ||
Vytvoří kopii aniž by ovlivnil aktuální instanci. | ||
/--php | ||
$created = DateTime::from('2017-02-03'); | ||
$public = $created->modifyClone('+1 day'); | ||
echo $created; // 2017-02-03 | ||
echo $public; // 2017-02-04 | ||
\-- | ||
|
||
implementuje JsonSerializable | ||
------------------------------ | ||
Vrátí řetězec ve standartu ISO 8601, který je používán třeba v JavaScriptu. | ||
/--php | ||
$date = DateTime::from('2017-02-03'); | ||
echo json_encode($date); | ||
\-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
Date and Time - Nette\Utils\DateTime | ||
************************************* | ||
|
||
.[perex] | ||
[Nette\Utils\DateTime |api:] is a static class extends [php:datetime] with useful functions for working with date and time. | ||
|
||
All examples assume the following class alias is defined: | ||
|
||
/--php | ||
use Nette\Utils\DateTime; | ||
\-- | ||
|
||
Let´s take a look how it´s simple. | ||
|
||
/--php | ||
$netteRelease = DateTime::from('2006-01-23 10:54:00'); // create from string | ||
$netteRelease->modify('+10 years, 1 month'); // add 10 years and 1 month | ||
echo $netteRelease; // 2016-02-23 10:54:00 | ||
\-- | ||
|
||
/--php | ||
$birthday = DateTime::createFromFormat('d.m.Y', '26.02.1994'); // create from custom format | ||
echo $birthday->getTimestamp(); // 762265863 | ||
\-- | ||
|
||
.[note] | ||
modify is method from parent class [php:datetime] | ||
|
||
from($time) | ||
---------- | ||
Create [Nette\Utils\DateTime |api:] instance from string, integer or other implementations of [php:datetime] and setup default timezone. | ||
|
||
/--php | ||
DateTime::from(1138013640); // create from timestamp | ||
DateTime::from(42); // create from current time and add 42 seconds | ||
DateTime::from('1994-02-26 04:15:32'); // create from date and time | ||
DateTime::from('1994-02-26'); // create from date then fill zeroes | ||
\-- | ||
|
||
createFromFormat($format, $time, $timezone = NULL) | ||
-------------------------------------------------- | ||
Extends //from// method for set custom format and timezone. | ||
/--php | ||
DateTime::createFromFormat('d.m.Y', '26.02.1994'); // create from custom format and default timezone | ||
DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create with custom timezone | ||
DateTime::createFromFormat('j. n. Y H:i', '8. 9. 2042 16:36'); // other example | ||
\-- | ||
|
||
setTimestamp($timestamp) | ||
------------------------ | ||
/--php | ||
$created = new DateTime(); | ||
$created->setTimestamp(1138013640); | ||
\-- | ||
|
||
getTimestamp() | ||
-------------- | ||
/--php | ||
$publicDate = DateTime::from('2017-02-03'); | ||
$publicDate->getTimestamp(); //1486076400 | ||
\-- | ||
|
||
modifyClone($modify = '') | ||
------------------------- | ||
Create independent copy of current instance. | ||
/--php | ||
$created = DateTime::from('2017-02-03'); | ||
$public = $created->modifyClone('+1 day'); | ||
echo $created; // 2017-02-03 | ||
echo $public; // 2017-02-04 | ||
\-- | ||
|
||
implements JsonSerializable | ||
--------------------------- | ||
Get string in standard ISO 8601 (used by JavaScript). | ||
/--php | ||
$date = DateTime::from('2017-02-03'); | ||
echo json_encode($date); | ||
\-- |