If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
वैसे तो PHP हमें बहुत से functions provide कराती है date और time के लिए but सबसे common और generally सबसे ज्यादा use किया जाने वाला function है date().
PHP date() function हमें readable form में timestamp (date with time) value return करता है according तो parameter . जिसे हम अपनी need के according customize भी कर सकते हैं।
date() function का use करके current date और time print कराने की कोशिश करते हैं।
File : date_time.php
<?php
echo 'Curent date time is : '. date('d-m-y h:i:s a');
?>
Example में आप जो date function में जो small lettersमें characters use किये गए हैं , इन्ही के according PHP हमें DATE / TIME return करती है। आगे बढ़ने से पहले इनके बारे में थोड़ा जानते है -
Character | Description |
---|---|
d | represents days of month (01 to 31) |
D | represents day name of week. (example : Mon, Tues, . . . Sun) |
l | represents day name of week. It works same as 'D' . |
m | represents months year (01 to 12) . |
M | represents month name of year (Example : Jan , feb, . . . Dec). |
y | represents year in two digits . (Example : 20 , 21..). |
Y | represents year in four digits (Example : 2020 , 2021 ..). |
h | represents hours (01 to 12). |
H | represents hours (01 to 24). |
i | represents minutes. |
s | represents seconds. |
a | represents am / pm. |
A | represents AM / PM. |
नीचे दिए गए examples को आप देखकर समझ सकते हैं कि PHP में किस तरह से Date / Time को handle करते हैं।
See Example
File : date_time2.php
<?php
echo 'Curent Date : '. date('d/m/y');
echo 'Curent Date : '. date('D-M-Y');
echo 'Curent Date : '. date('d.M.Y');
echo 'Curent Date : '. date('d/m/y');
echo 'Curent Time : '. date('h:i:s a');
echo 'Curent Time : '. date('H:i:s A');
echo 'Curent Time : '. date('h:i:s A');
echo 'Curent Date Time : '. date('d/M/Y h:i:s A');
?>
इसके अलावा आप predefined date को customize भी कर सकते हैं , इसके लिए PHP ने हमें strtotime() function provide किया है जिसकी help से हम दी गयी date / time को need के according get कर सकते हैं।
See Example
File : date_time3.php
<?php
$date = '1990-11-23';
echo "Date : ".date('d/M/Y', strtotime($date));
echo "Day : ".date('d', strtotime($date));
echo "Day Name : ".date('D', strtotime($date));
?>
ऊपर दिए गए example में जिस तरह से हमने दी गयी date में से day / day name / date को extract किया उसी तरह से दिए गए time से भी हम strtotime() function use करके hour या minute extract कर सकते हैं।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook