← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 1 of 24

PHP Date और Time

PHP के date और time tools एक calendar और clock combined जैसे काम करते हैं। आप आज की date माँग सकते हैं, इसे किसी भी style में दिखा सकते हैं, या पता लगा सकते हैं कि एक हफ्ते में कौन सा दिन होगा।
Syntax
php
echo date("Y-m-d H:i:s");                // format the current time
$timestamp = strtotime("+3 days");
$dt = new DateTime("date string");
echo $dt->format("Y-m-d");

Current Date और Time पाना

date() function current time (या आपके दिए किसी Unix timestamp) को letter codes के एक format string के according format करता है, इसलिए date(Y-m-d) आपको तुरंत एक sortable ISO-style date देता है।

उदाहरण: Getting the Current Date and Time

php
<?php
// Print `date('Y-m-d')` to the output
echo date('Y-m-d');
?>

Strings से Timestamps बनाना

strtotime() 'next Monday' या '+3 days' जैसे everyday English phrases को एक Unix timestamp में parse करता है, जो user-typed dates को कुछ ऐसा बनाने के लिए remarkably flexible है जिससे PHP calculate कर सके।

उदाहरण: Creating Timestamps from Strings

php
<?php
// Declare `$timestamp`, set to `strtotime("+3 days")`
$timestamp = strtotime("+3 days");
// Print `date('Y-m-d', $timestamp)` to the output
echo date('Y-m-d', $timestamp);
?>

DateTime Class के साथ काम करना

DateTime class date handling को एक object-oriented API में wrap करता है जो leap years और daylight-saving shifts जैसे tricky edge cases को सही से handle करता है, जिन्हें manual timestamp math अक्सर गलत करता है।

उदाहरण: Working with the DateTime Class

php
<?php
// Create a new `DateTime` instance with '2024-02-29', stored in `$date`
$date = new DateTime('2024-02-29');
// Print `$date->format('Y-m-d')` to the output
echo $date->format('Y-m-d');
?>

Dates Modify करना

DateTime का modify() method और अलग DateInterval class दोनों आपको एक date को एक readable amount (जैसे '+1 month') से आगे या पीछे shift करने देते हैं, जो raw seconds add करने से कहीं कम error-prone है।

उदाहरण: Modifying Dates

php
<?php
// Create a new `DateTime` instance with '2024-01-01', stored in `$date`
$date = new DateTime('2024-01-01');
$date->modify('+1 month');
// Print `$date->format('Y-m-d')` to the output
echo $date->format('Y-m-d');
?>

PHP में Timezones

date_default_timezone_set() उस timezone को fix करता है जिसे आपकी पूरी script मानती है जब कुछ और न बताया जाए; इसके बिना, PHP एक server default पर fall back करता है जो आपके users की उम्मीदों से match न हो।

उदाहरण: Timezones in PHP

php
<?php
// Call `date_default_timezone_set('America/New_York')`
date_default_timezone_set('America/New_York');
// Print `date('Y-m-d H:i:s')` to the output
echo date('Y-m-d H:i:s');
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. date_default_timezone_set से time zone set न करना, ताकि dates एक unexpected default इस्तेमाल करें।
  2. date format strings में m (month) और i (minutes) को confuse करना, जैसे Y-m-d H:m:s।
  3. यह मान लेना कि DateTime::modify एक नया object return करता है, जबकि यह object को in place बदलता है; इससे बचने के लिए DateTimeImmutable इस्तेमाल करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.