-2

I'm formatting date in php. But it's returning current year.

This is my code:

<?php
     $date = "2 January, 2018";
     echo date('d-m-Y', strtotime( $date ));
?>

I need date format in d-m-Y format.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Sunil tc
  • 2,482
  • 4
  • 19
  • 46
  • 1
    If you actually look at the timestsamp that's returned, you'll see that `strtotime()` treats the comma as a separator between the date and the time, so this looks like a date with no year (and it will default to the current year), but with a time of `20:18` – Mark Baker Nov 22 '17 at 13:07
  • The issue with a comma in the string is covered here: https://stackoverflow.com/questions/1884509/php-strtotime-returning-wrong-results – iainn Nov 22 '17 at 13:09

2 Answers2

1

Remove Comma

<?php
     $date = "2 January 2018";
     echo date('d-m-Y', strtotime( $date ));
?>
Aman Maurya
  • 1,305
  • 12
  • 26
0

Remove the comma from the date string.

John Corry
  • 1,567
  • 12
  • 15
  • http://php.net/manual/en/function.date.php "m" is numeric of month – Andreas Nov 22 '17 at 13:07
  • 1
    @Andreas - What has your comment got to do with this answer? – Mark Baker Nov 22 '17 at 13:08
  • Yes, `m` is numeric for month...and if you take the comma out of the date string he's using, `strtotime` can parse all of it, including the year. – John Corry Nov 22 '17 at 13:10
  • @mark if you remove the comma the string will be `2 January, 2018` and not `2-1-2018` as OP requests. – Andreas Nov 22 '17 at 13:13
  • 1
    @Andreas - If you remove the comma, then `$date = "2 January, 2018";` will be changed to `$date = "2 January 2018";`, and the date conversion and formatting will work correctly.... as [shown here](https://3v4l.org/g9VS8) – Mark Baker Nov 22 '17 at 13:14
  • no, if you remove the comma, `strtotime` can correctly parse the date (with the year), so the date being formatted will (correctly) have 2018 in the year. The format string 'd-m-Y' will handle the formatting. – John Corry Nov 22 '17 at 13:14
  • I think @Andreas either doesn't understand the question or has never formatted a date in PHP. – John Corry Nov 22 '17 at 13:15
  • Or maybe you need to learn how to properly answer a question. An answer should include (if not impossible) code how to fix the issue. Not a one liner of text. In my opinion. – Andreas Nov 22 '17 at 13:18
  • @Andreas you need code to understand what "remove the comma from the date string" means? – John Corry Nov 22 '17 at 13:19
  • One day you'll grow up and Lear how to answer questions and how to find then duplicates instead of doing one liners of text. – Andreas Nov 22 '17 at 13:20
  • @Andreas That doesn't make your original comment any less meaningless.... it has absolutely nothing to do with `m` in the `date()` format – Mark Baker Nov 22 '17 at 13:23
  • If "remove the comma" is an acceptable answer then would "change the placing a of the numbers" be an acceptable answer if someone wants to change from "Y-m-d" to "d-m-Y"? – Andreas Nov 22 '17 at 13:27