Which PHP function can return the current date/time?
PHP's time() returns a current Unix timestamp. With this, you can use the date() function to format it to your needs.
$date = date('Format String', time());
As Paolo mentioned in the comments, the second argument is redundant. The following snippet is equivalent to the one above:
$date = date('Format String');
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('d-m-Y H:i:s');
Update
//Also get am/pm in datetime:
echo $date->format('d-m-Y H:i:s a'); // output 30-12-2013 10:16:15 am
For the date format, PHP date() Function is useful.
echo date("d-m-Y H:i:sa");
This code will get the date and time of the server that the code runs on.
Normally, this function for date is useful for everyone: date("Y/m/d");
But time is something different, because the time function depends on either the PHP version or system date.
So probably use it like this to get our own time zone:
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('H:m:s');
This function shows the 24 hours time.
For the new PHP programmer might confuse why there are lot of method for to get current date and time and which one to use in their project.
1. date
method (PHP 4, PHP 5, PHP 7)
This is the very common and very easiest way to get the date and time in php.
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
You can learn more about it in here
2. DateTime
class (PHP 5 >= 5.2.0, PHP 7)
when you want to use PHP with OOP, this is the best way to get date and time.
<?php
// Specified date/time in your computer's time zone.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Specified date/time in the specified time zone.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in your computer's time zone.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Using a UNIX timestamp. Notice the result is in the UTC time zone.
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
You can learn more about it in here
3. Carbon Date time package
if you are using Composer, Laravel, Symfony or any kinda framework this is the best way to get the date and time. Also this package extends DateTime class in php so you use all the method in Datetime class. This in-built in frameworks like laravel so you don't have to install it separately.
printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); // automatically converted to string
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
// Carbon embed 823 languages:
echo $tomorrow->locale('fr')->isoFormat('ffffdd, MMMM Do YYYY, h:mm');
echo $tomorrow->locale('ar')->isoFormat('ffffdd, MMMM Do YYYY, h:mm');
$officialDate = Carbon::now()->toRfc2822String();
$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
$internetWillBlowUpOn = Carbon::create(2038, 01, 19, 3, 14, 7, 'GMT');
if (Carbon::now()->isWeekend()) {
echo 'Party!';
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
You can learn more about it in here
Hope this helps and if you know any other way to get the date and time feel free to edit the answer.
Very simple
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y H:i:s', time());