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

Asked

Viewed 87 times

0

I used a cURL, to make a api, which picked a specific date.

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

How could I format this for "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
  • 1

    Hello, young man. Don’t edit the question that way. You pretty much took out the context answers when making this issue. I reversed. If you have another question, ask another question.

2 answers

1


Simply use the class DateTime.

new DateTime('September 18, 2018')

Upshot:

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

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

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

Upshot:

 "18/09/2018"

To the class builder DateTime and the function strtotime interprets a string and turns it into date.

See working on Ideone

-2

experiment

<?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);
  • 2

    -1 How does this answer the question? Where is the date parameter that AP wants to convert?

  • 1

    I suggest clicking on the "edit" option and adding information to your reply. I will consider removing my negative vote.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.