0

In the XML file, the date format is being received as: 20140327163000 (YYYYMMDDHHMINSEC). I need to parse the date and convert it into the following format (DD/MM/YYYY HH:MIN:SS) before posting it into the database. The following is the code I am using, however it is not posting anything in the database:

<?php 

// Create connection
$con=mysqli_connect("localhost","test","test","epg");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$dir = "xml-files/";
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (($file !== '.') && ($file !== '..') ) {
    $doc = simplexml_load_file($dir . $file); 

foreach ( $doc->ScheduleData->ChannelPeriod as $channelPeriod )
{
   $channelId = $channelPeriod->ChannelId;

   foreach ( $channelPeriod->Event as $event )
   {
      $beginTime = $event['beginTime'];
      $duration = $event['duration'];
      $programName = $event->EpgProduction->EpgText->Name;
      $description = $event->EpgProduction->EpgText->Description;
      $EventId = $event->EventId;

         $format = 'd-m-Y H:i:s';
        $date = DateTime::createFromFormat($format, '$beginTime');

      $sql = "insert into `epg` (`EventId`,`ChannelId`, `BeginTime`,`Duration`, `ShortName`, `Description`) values ('$EventId','$channelId', '$date','$duration', '$programName', '$description')";

        if (mysqli_query($con,$sql))
      {
      echo "Database updated successfully<br />";
      }
    else
      {
      echo "Error creating database: " . mysqli_error($con)."<br />";
      }


                    }
                }
            }
        }
  closedir($dh);
    }
}

$sql_delete = "DELETE FROM `EPG` WHERE BeginTime < ";

function deleteFiles($dir) {
    $files = glob($dir);
    foreach($files as $file){ 
      if(is_file($file))
        unlink($file); 
    }
}

deleteFiles("xml-files/*");

?>
5
  • did you check what value is in $event['beginTime']? do empty values get inserted? which condition is executed here if (mysqli_query($con,$sql))? Commented Mar 27, 2014 at 16:53
  • @Darius the value of $event['beginTime'] is constantly changing but always in the format of 20140327163000 (YYYYMMDDHHMINSEC) Commented Mar 27, 2014 at 16:56
  • so is your problem just parsing it to the right format? Commented Mar 27, 2014 at 16:57
  • @Darius exactly, because if in the SQL statement I put in the variable $beginTime it works perfect but in the format mentioned above, I would like to post the format of DD/MM/YYYY HH:MIN:SS in the database Commented Mar 27, 2014 at 16:59
  • $date = DateTime::createFromFormat($format, '$beginTime'); that seems to be operating on the string '$beginTime' not the variable $beginTime. Lose the single quotes? Commented Mar 27, 2014 at 18:35

1 Answer 1

1

Try this piece and see if it works for you:

$date = new DateTime(20140327163000);
var_dump($date->format('d/m/Y H:i:s'));

EDIT:

So, instead of the value 20140327163000 use a variable that holds the value you want to save (and don't use var_dump of course) - this was just to present that this piece of code would parse the value given to the string formatted as you want.

$date = new DateTime($yourValue);
$dateToSave = $date->format('d/m/Y H:i:s');
Sign up to request clarification or add additional context in comments.

3 Comments

The value in this $date = new DateTime(20140327163000); constantly changes it is not always 20140327163000
@Darius.... this works perfectly fine many thanks, now how can I add the functionality to convert the time only in a particular format lets say GMT+1 etc...?
please check documentation for DateTime class here php.net/manual/en/book.datetime.php, also if the solution works for you please accept the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.