Php, How to Get Current Date in Certain Format

PHP, How to get current date in certain format

date('Y-m-d H:i:s'). See the manual for more.

How to get current date in certain format with PHP?

Please try following code :

echo "current time: " .date('Y-m-d h:i:s');

echo "<br>current timestamp minus 15 minutes :". date('Y-m-d H:i:s', strtotime('-15 minutes'));

NOW() function in PHP

You can use the date function:

date("Y-m-d H:i:s");

Get the current date and time in PHP with '2016-07-04 00:00:00.000' format

Use date->format http://php.net/manual/it/function.date.php

$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');

How do I display the next 3 dates from today in a particular format in php?

You can easily do it with date() and for() loop:-

<?php

date_default_timezone_set('AFRICA/LAGOS');

$date = date('Y-m-d');
for($i =1;$i<=3;$i++){
echo $end_date = date('Y M,d', strtotime("+$i days"));
echo PHP_EOL;
}

https://3v4l.org/YXZEe

A bit functional approach:

<?php

function getNextDatesFromCurrentDate($how_many_dates){
date_default_timezone_set('AFRICA/LAGOS');

for($i =1;$i<=$how_many_dates;$i++){
echo $end_date = date('Y M,d', strtotime("+$i days"));
echo PHP_EOL;
}
}

getNextDatesFromCurrentDate(3);

https://3v4l.org/1vKVA

Convert a date format in PHP

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(See the strtotime and date documentation on the PHP site.)

Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format :-)

display date format

Just replace the hardcoded date with your value

$timestamp = strtotime('2-March-2011');
$newDate = date('d-F-Y', $timestamp);
echo $newDate; //outputs 02-March-2011

Get location date format

here you can check what Carbon does for each ISO format:

https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Traits/Date.php#L1907

Some of them have equivalent in DateTime::format if this is the format you expect as an output but all won't have an equivalent code. For instance DateTime::format has a format for the ordinal st / nd / th but it's only in English, while the ISO format Do means day number with ordinal of the current language (can be inci for tr_TR locale).

This means you can make an approximated mapping of what ISO format is for DateTime::format, but this won't be an exact match.



Related Topics



Leave a reply



Submit