Home > Enterprise >  format date and time output by adding 1 hour php
format date and time output by adding 1 hour php

Time:11-22

I'm writing this plugin which shows some battle info. I'm not an expert, I'm learning now. when I print the battle time it shows as 2021-11-12 21: 15: 00.000000 and I would like to change it to 2021-11-12 21:15 1 hours then 2021-11-12 22:15 PS the date is an event and can change over time. how can I do? someone help me thanks`

    // If this file is access directly, abort!!!
defined( 'ABSPATH' ) or die( 'Unauthorized Access' );

// Action when user login into admin panel
add_shortcode( 'external_data', 'callback_function_name' );

function callback_function_name( $atts) {

    // return;

    $defaults =[
         'title'=>'table title'
    ];

    $atts = shortcode_atts( 
        $defaults,
        $atts,
        'external_data'
    );

    $url = 'http://localhost/wp/wp-content/plugins/personal-widget/testjson.json';      
    $response = wp_remote_get( $url );

    if ( is_wp_error( $response ) ) {
        $error_message = $response->get_error_message();
        return "Something went wrong: $error_message";
    } 
        
    $json=json_decode( wp_remote_retrieve_body( $response),true ) ;    
        
    //qui abbiamo creato la tabella dei dati
    $html = '';
    $html .= '<h2>' .$atts ['title']. '</h2>';
    $html .= '<table>';
    $html .= '<tr>';
    $html .= '<td>Oraio Battaglia</td>';
    $html .= '<td>Tipo</td>';
    $html .= '<td>Provincia</td>';
    $html .= '<td>Arena</td>';
    $html .= '</tr>';
  
    foreach ($json['planned_battles'] as $p_battle) {   
     
         $html .= '<tr>';
         $html .= '<td>' . $p_battle['battle_time'] .'</td>';
         $html .= '<td>' . $p_battle['province_type'] .'</td>';
         $html .= '<td>' . $p_battle['province_id'] .'</td>';
         $html .= '<td>' . $p_battle['arena_name'] .'</td>';
         $html .= '</tr>';
    }    

   $html .= '</table>';
   // questo mostra la tabella html
   return $html;    
}   

this is a json url

    {
  "clan": {
    "elo_rating_6": 1000,
    "elo_rating_10": 832,
    "name": "SQUADRONE CORAZZATO ITALIANO",
    "color": "#00a0e6",
    "elo_rating_8": 1009,
    "tag": "SQCI",
    "appointed_battles_count": 3,
    "id": 500057125,
    "emblem_url": "***/clans/emblems/cl_125/500057125/emblem_64x64_gm.png",
    "fine_level": 0
  },
  "planned_battles": [
    {
      "battle_time": "2021-11-12 21:15:00.000000",
      "is_attacker": true,
      "province_revenue": 0,
      "province_id": "donbenito",
      "winner_id": null,
      "province_type": "landing",
      "battle_reward": null,
      "attack_type": "TOURNAMENT",
      "arena_resp_number": null,
      "province_owner_id": 500173820,
      "clan": {
        "division_id": null,
        "arena_wins_percent": 0,
        "arena_battles_count": 0,
        "win_rating_delta": null,
        "lose_rating_delta": null
      },
      "enemy": null,
      "arena_name": "Malinovka",
      "landing": true,
      "revenue_level": 0,
      "periphery": "EU2",
      "bets_slice_time": null,
      "front_id": "season_17_eu_tier10m",
      "round_number": null,
      "province_pillage_end_datetime": null,
      "province_name": "Don Benito"
    },
    {
      "battle_time": "2021-11-12 21:00:00.000000",
      "is_attacker": true,
      "province_revenue": 0,
      "province_id": "tlemcen",
      "winner_id": null,
      "province_type": "landing",
      "battle_reward": null,
      "attack_type": "TOURNAMENT",
      "arena_resp_number": null,
      "province_owner_id": 500002273,
      "clan": {
        "division_id": null,
        "arena_wins_percent": 0,
        "arena_battles_count": 0,
        "win_rating_delta": null,
        "lose_rating_delta": null
      },
      "enemy": null,
      "arena_name": "Steppes",
      "landing": true,
      "revenue_level": 0,
      "periphery": "EU2",
      "bets_slice_time": null,
      "front_id": "season_17_eu_tier10m",
      "round_number": null,
      "province_pillage_end_datetime": null,
      "province_name": "Tlemcen"
    }
  ],
  "battles": [
    {
      "battle_time": "2021-11-12 20:15:01.500000",
      "is_attacker": true,
      "province_revenue": 0,
      "province_id": "lyon",
      "winner_id": null,
      "province_type": "landing",
      "battle_reward": null,
      "attack_type": "TOURNAMENT",
      "arena_resp_number": 1,
      "province_owner_id": 500211636,
      "clan": {
        "division_id": 2740559,
        "arena_wins_percent": 40.51,
        "arena_battles_count": 237,
        "win_rating_delta": 12,
        "lose_rating_delta": -3
      },
      "enemy": {
        "division_id": 2740562,
        "elo_rating_6": 1000,
        "elo_rating_10": 1111,
        "name": "-=Hungary-Shadow-Killers=-",
        "arena_wins_percent": 51.2,
        "color": "#8bff26",
        "win_rating_delta": 3,
        "elo_rating_8": 956,
        "arena_battles_count": 125,
        "tag": "HULKS",
        "lose_rating_delta": -12,
        "id": 500137692,
        "emblem_url": "***/clans/emblems/cl_692/500137692/emblem_64x64_gm.png",
        "fine_level": 0
      },
      "arena_name": "Live Oaks",
      "landing": true,
      "revenue_level": 0,
      "periphery": "EU2",
      "front_id": "season_17_eu_tier10m",
      "round_number": 1,
      "province_pillage_end_datetime": null,
      "province_name": "Lyon"
    }
  ]
}

output image

CodePudding user response:

You need to convert that string into a Datetime object in order to add the hour. Try something like this:

$battleTime = Datetime::createFromFormat('Y-m-d H:i:s.u', $p_battle['battle_time']);

// This will add one hour to your date
$battleTime->add(new DateInterval('PT1H'));

echo $battleTime->format('Y-m-d H:i:s.u')

That should do it, if you have any more questions just let me know. Happy coding!

  • Related