-1

XML is outputting time like this:

<time from="2020-02-03T12:00:00" to="2020-02-03T18:00:00" period="2"

^ this is dynamic and changes all the time ^

I would want to display it like this:

  1. Feb 12:00 - 18:00

This is my PHP:

$url = ('https://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml');

  $feed = simplexml_load_file($url) or die('Can not connect to server');
  $result = array();
  foreach ($feed->forecast->tabular->time as $content) {
      array_push($result, [ "from" => (string)$content['from'],
          "to" => (string)$content['to'],
          'symbol' => (string)$content->symbol['name'],
          'temperature' => (string)$content->temperature['value'],
          'windDirection' => (string)$content->windDirection['code'],
          'windSpeed' => (string)$content->windSpeed['mps'],
      ]);
  }

This is my html:

<section>
  <div class="tbl-header">
    <table cellpadding="0" cellspacing="0" border="0">
      <thead>
        <tr>
          <th>Time</th>
          <th>Weather</th>
          <th>Temperature</th>
          <th>Wind</th>
        </tr>
      </thead>
    </table>
  </div>
  <div class="tbl-content">
    <table cellpadding="0" cellspacing="0" border="0">
      <tbody>
        <?php foreach ($result as $value) { ?>
          <tr>
             <td>Bergen <br /><?php echo $value['from'] ?> til  <?php echo $value['to'] ?></td>
             <td><?php echo $value['symbol'] ?></td>
             <td><?php echo $value['temperature'] ?> °C</td>
             <td><?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
         </tr>
        <?php } ?>
      </tbody>
    </table>
  </div>
</section>

how can I do that the best way by using php? Any help is greatly appreciated.

Storm1
  • 199
  • 1
  • 1
  • 10
  • One of the most important skills in programming is breaking problems down: you can ignore the fact that the string comes from XML, and ignore the fact that it's going to HTML; then your problem becomes "how do I change a date from the format `2020-02-03T12:00:00` to the format `Feb 12:00`?" – IMSoP Feb 03 '20 at 12:30

1 Answers1

0

UPDATE : Simple solution With php self.

<td>Bergen <br /><?php echo date("j M, g:i", strtotime($value['from'])); ?> - <?php echo date("g:i", strtotime($value['to'])); ?></td>

Result : 3 Feb, 2:00 - 6:00

More examples : https://www.php.net/manual/en/function.date.php

With custom function This function shortens months names

 function shorten_text($text, $max){
    $text = substr($text, 0, $max);
    $text_son = strrchr($text, " ");
    $text = str_replace($text_son,"", $text);      
    return $text;
} 

You need to format date like this

<td>Bergen <br /><?php echo date('d', strtotime($value['from'])).".". shorten_text(date("F", strtotime($value['from'])), 3) ." ". date('H:i', strtotime($value['from']));?> til  <?php echo date('d', strtotime($value['to'])).".". shorten_text(date("F", strtotime($value['to'])), 3) ." ". date('H:i', strtotime($value['to']));?></td>

This will give you result like 03.Feb 13:00 til 03.Feb 18:00. play with it and turn as you like.

  • There's no need for a custom function to shorten the month name, just use `M` ("A short textual representation of a month, three letters") instead of `F` ("A full textual representation of a month, such as January or March") in your date format, as documented in the manual: https://www.php.net/manual/en/function.date.php – IMSoP Feb 03 '20 at 12:33
  • 1
    Yes there is if you are not english! it doesnt display month names in my language. –  Feb 20 '20 at 09:27
  • Are you saying that the F format is translated, but the M format isn't? – IMSoP Feb 22 '20 at 11:20