-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy path09_dates.php
More file actions
34 lines (26 loc) · 958 Bytes
/
09_dates.php
File metadata and controls
34 lines (26 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// 1. Print current timestamp
echo time() . '<br>';
// 2. Print current date
echo date('Y-m-d H:i:s') . '<br>';
// 3. Print yesterday
echo date('Y-m-d H:i:s', time() - 60 * 60 * 24) . '<br>';
// 4. Different format: https://www.php.net/manual/en/function.date.php
echo date('F j Y, H:i:s') . '<br>';
// 5. String to timestamp
echo strtotime('now') . "<br>";
echo strtotime('+1 day') . "<br>";
echo strtotime('+1 week') . "<br>";
// 6. Parse date: https://www.php.net/manual/en/function.date-parse.php
$dateString = '2020-02-06 12:45:35'; // Declare date
$parsedDate = date_parse($dateString); // Parse date
echo '<pre>';
var_dump($parsedDate);
echo '</pre>';
// 7. Parse date from format
// https://www.php.net/manual/en/function.date-parse-from-format.php
$dateString = 'February 4 2020 12:45:35'; // With non-default format
$parsedDate = date_parse_from_format('F j Y H:i:s', $dateString);
echo '<pre>';
var_dump($parsedDate);
echo '</pre>';