pranay kumar
pranay kumar

Reputation: 11

PHP date format(I want convert date (Y-m-d) to (Y,m,d) format

<?php 
$dd=2021-8-28;
 date_default_timezone_set('UTC');
 $repl=str_replace('-', ',', $dd);
 $newdat=date("Y,m,d",strtotime($repl));
 echo $newdat;
?>

///output=1997,01,01 but It should be 2021,08,28

Upvotes: 1

Views: 216

Answers (2)

Huzaifa Qidwai
Huzaifa Qidwai

Reputation: 239

try this

 dd='2021-8-28';

 date_default_timezone_set('UTC');

 $newdat=date("Y,m,d",strtotime($dd));

 echo $newdat;

Upvotes: 0

George
George

Reputation: 158

I'm not sure why you're getting 1997 in the output, honestly; when I try your code out on my machine, I get 1985, which is what I would expect -- $dd isn't a date right now, it's a subtraction problem! :) 2021-8-28 == 1985

Make $dd a string: (note the added leading zero, to make the date string ISO-compliant)

$dd="2021-08-28";

Then you can turn it into a date using the date() function; you don't need to do the string replace and can take that line out, as you've already specified to use commas as separators in the first parameter of your date() function.

$newdat=date("Y,m,d",strtotime($dd));

Upvotes: 1

Related Questions