How to convert date into "September 18, 2018" format in PHP?

0

I used a cURL , to make an api , in which I would get a specific date.

The problem is that the content returned is in English, for example: September 18, 2018 .

How could I format this to "en"?

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
$espdata = GetStr($data,'<exemplo>','</exemplo>');
echo $espdata; //September 18, 2018
    
asked by anonymous 24.08.2018 / 18:36

2 answers

1

Simply use the DateTime class.

new DateTime('September 18, 2018')

Result:

DateTime {#168
     +"date": "2018-09-18 00:00:00.000000",
     +"timezone_type": 3,
     +"timezone": "America/Sao_Paulo",
   }

If you like to use the date function, you can combine with strtotime :

date('d/m/Y', strtotime('September 18, 2018'))

Result:

 "18/09/2018"

A constructor of class DateTime and function strtotime interprets a string and transforms it into a date.

See working at Ideone

    
24.08.2018 / 18:39
-2

try

<?php
date_default_timezone_set('America/Sao_Paulo');

$data = new DateTime();
$formatter = new IntlDateFormatter('pt_BR',
                                    IntlDateFormatter::FULL,
                                    IntlDateFormatter::NONE,          
                                    IntlDateFormatter::GREGORIAN);
echo $formatter->format($data);
    
24.08.2018 / 18:40