PHP Date और Time
In this page:
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
// Print `date('Y-m-d')` to the output
echo date('Y-m-d');
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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');
?>
Login to try C/C++/Java/PHP code in the editor
Dates Modify करना
DateTime का modify() method और अलग DateInterval class दोनों आपको एक date को एक readable amount (जैसे '+1 month') से आगे या पीछे shift करने देते हैं, जो raw seconds add करने से कहीं कम error-prone है।
उदाहरण: Modifying Dates
<?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');
?>
Login to try C/C++/Java/PHP code in the editor
PHP में Timezones
date_default_timezone_set() उस timezone को fix करता है जिसे आपकी पूरी script मानती है जब कुछ और न बताया जाए; इसके बिना, PHP एक server default पर fall back करता है जो आपके users की उम्मीदों से match न हो।
उदाहरण: Timezones in 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');
?>
Login to try C/C++/Java/PHP code in the editor
date_default_timezone_setसे time zone set न करना, ताकि dates एक unexpected default इस्तेमाल करें।- date format strings में
m(month) औरi(minutes) को confuse करना, जैसेY-m-d H:m:s। - यह मान लेना कि
DateTime::modifyएक नया object return करता है, जबकि यह object को in place बदलता है; इससे बचने के लिएDateTimeImmutableइस्तेमाल करें।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser