Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a date string in PHP

Tags:

date

php

If I have a string which represents a date, like "2011/07/01" (which is 1st July 2011) , how would I output that in more readable forms, like:

1 July 2011
1 Jul 2011  (month as three letters)

And also, how could I make it intelligently show date ranges like "2011/07/01" to "2011/07/11" as

1 - 11 July 2001

(without repeating the 'July' and '2011' in this case)

like image 952
cannyboy Avatar asked Sep 06 '10 20:09

cannyboy


People also ask

How can get date in dd mm yyyy format in PHP?

In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate));

What does date () do in PHP?

PHP date() Function The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.

What is default format of date in PHP?

Mysql default date format is Year-Month-Date that is why every PHP developer has with a problem. $date = "2019-02-08" ; $newDate1 = date ( "m-d-Y" , strtotime ( $date )); $newDate2 = date ( "l M, d, Y" , strtotime ( $date ));

What does Strtotime mean in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


3 Answers

You can convert your date to a timestamp using strtotime() and then use date() on that timestamp. On your example:

$date = date("j F Y", strtotime("2011/07/01")); // 1 July 2011
$date = date("j M Y", strtotime("2011/07/01")); // 1 Jul 2011
like image 54
NullUserException Avatar answered Oct 18 '22 22:10

NullUserException


As NullUserException mentioned, you can use strtotime to convert the date strings to timestamps. You can output 'intelligent' ranges by using a different date format for the first date, determined by comparing the years, months and days:

$date1 = "2011/07/01";
$date2 = "2011/07/11";

$t1 = strtotime($date1);
$t2 = strtotime($date2);

// get date and time information from timestamps
$d1 = getdate($t1);
$d2 = getdate($t2);

// three possible formats for the first date
$long = "j F Y";
$medium = "j F";
$short = "j";

// decide which format to use
if ($d1["year"] != $d2["year"]) {
    $first_format = $long;
} elseif ($d1["mon"] != $d2["mon"]) {
    $first_format = $medium;
} else {
    $first_format = $short;
}

printf("%s - %s\n", date($first_format, $t1), date($long, $t2));
like image 37
Richard Fearn Avatar answered Oct 18 '22 21:10

Richard Fearn


As for the second one:

$time1 = time();
$time2 = $time1 + 345600; // 4 days
if( date("j",$time1) != date("j",$time2) && date("FY",$time1) == date("FY",$time2) ){
   echo date("j",$time1)." - ".date("j F Y",$time2);
}

Can be seen in action here

Just make up more conditions

like image 1
Robus Avatar answered Oct 18 '22 22:10

Robus