I need some help with set up the date format.

I'm still trying to find out how I can set up the format to the next day, the day two and the day three date when I work out with the hours.

Example: I set up the date format from today and the time is 10:00 AM, 11:00 AM, 12:00 PM, the format is 20140424100000, 20140424110000, 20140424120000 and so on until when I reaction to the 12:00 AM, I want to set up the format to the next day 20140425000000, 20140425003000, 20140425020000, 20140425023000 until when I reaction to the next 12:00 AM I want to set up the new format to the day two 20140426000000, 20140426003000, 20140426010000.

My problem is when I'm parsing the list of time strings from my script, how I can work out the format with the date and the time?

Here is the PHP:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
include ('simple_html_dom.php');

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];
  $html_two = file_get_html("http://www.mysite.com/myscript.php?getime);

  //time1
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;

  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  $time1 = explode(" ", $time1);
  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];


  //time2
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;

  $hoursMinutes = explode(":", $time2[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  $time2 = explode(" ", $time2);
  $hoursMinutes = explode(":", $time2[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];


  //time3
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;

  $hoursMinutes = explode(":", $time3[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  $time3 = explode(" ", $time3);
  $hoursMinutes = explode(":", $time3[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];
?>

Here is the time strings:

10:00 AM

11:00 AM

12:00 PM

12:30 PM

2:00 PM

2:30 PM

9:00 PM

11:00 PM

12:00 AM

12:30 AM

1:00 AM

1:30 AM

2:00 AM

11:00 PM

12:00 AM

12:30 AM

1:00 AM

Hi, I'm struggling to see exactly what you're after here.

Is http://www.mysite.com/myscript.php?getime returning "10:00 AM" and you want to convert it to 20140424100000?

If so I'd probably go with somthing like:

$timeString = "10:00 AM";
$dayIncrement = 0;

$myYear = 2014;
$myMonth = 4;
$myDay = 24;

//separate the time from AM/PM
$timeArray = explode(" ",$timeString);

//separate hours and minutes
$timeArray[0] = explode(":",$timeArray[0]);

//make it a 24 hour clock
if (($timeArray[1] == "PM") && ($timeArray[0][0] < 12)){
    $timeArray[0][0] +=  12;
}

//if the hour is 12 AM this is a special time as it is midnight
if (($timeArray[1] == "AM") && ($timeArray[0][0] == 12)) {
    $timeArray[0][0] =  0;
    //I am of the impression that this triggers a new day in your requirements
    $dayIncrement = 1;
}

$myNewTimeString =  str_pad((string) $timeArray[0][0], 2, "0", STR_PAD_LEFT);
$myNewTimeString .= (string) $timeArray[0][1];
$myNewTimeString .= "00"; //we don't have the seconds to use.

//0 pad
$myMonth = str_pad((string) $myMonth, 2, "0", STR_PAD_LEFT);

//increment day if necessary
$myDay += $dayIncrement;
$myDay = str_pad((string) $myDay, 2, "0", STR_PAD_LEFT);

//build the date string
$myDateString = (string) $myYear;
$myDateString .= $myMonth . $myDay . $myNewTimeString;

echo $myDateString;

I haven't tested this and like I said I'm not sure what your actual requirements are :) but assuming this is the sort of thing you're after it shouldn't be too hard to build it into a function/class.

Happy coding :)
Ben

@Ben, yes that is what I want to convert the 10:00AM to 20140424100000.

How do you convert the list of time into format using $time variable:

10:00 AM

11:00 AM

12:00 PM

12:30 PM

2:00 PM

2:30 PM

9:00 PM

11:00 PM

12:00 AM

12:30 AM

1:00 AM

1:30 AM

2:00 AM

11:00 PM

12:00 AM

12:30 AM

1:00 AM

to make it return like this?

20140424100000

20140424110000

20140424120000

20140424123000

20140424140000 

20140424143000

20140424210000

20140424230000 

20140425000000

20140425003000

20140425010000

20140425013000

20140425020000

20140425230000

20140426000000

20140426003000

20140426010000

Here is the output:

<span id="time1">10:00 AM</span> <span id="time2">11:00 AM</span> <span id="time3">12:00 PM</span> 
<span id="time4">12:30 PM</span> <span id="time5">2:00 PM</span> <span id="time6">2:30 PM</span> 
<span id="time7">9:00 PM</span> <span id="time8">11:00 PM</span> <span id="time9">12:00 AM</span> 
<span id="time10">12:30 AM</span> <span id="time11">1:00 AM</span> <span id="time12">1:30 AM</span> 
<span id="time13">2:00 AM</span> <span id="time14">11:00 PM</span> <span id="time15">12:00 AM</span> 
<span id="time16">12:30 PM</span> <span id="time17">1:00 AM</span>
Member Avatar for diafol
<?php

$timeList = array(
    "10:00 AM",
    "11:00 AM",
    "12:00 PM",
    "12:30 PM",
    "2:00 PM",
    "2:30 PM",
    "9:00 PM",
    "11:00 PM",
    "12:00 AM",
    "12:30 AM",
    "1:00 AM",
    "1:30 AM",
    "2:00 AM",
    "11:00 PM",
    "12:00 AM",
    "12:30 AM",
    "1:00 AM"
);

    $startDate = '2014-04-24';


function getNewDateTimes($timeList, $startDate) 
{   
    $newDateTimes = array();

    $dt = new DateTime($startDate . ' 00:00:00');

    $firstPeriod = substr(trim($timeList[0]),-2);
    $rollingPeriod = $firstPeriod;
    foreach($timeList as $time)
    {
        $p = substr(trim($time),-2);
        if($p != $rollingPeriod)
        {
            if($p == "AM") $dt->add(new DateInterval('P1D'));
            $rollingPeriod = $p;     
        }
        $nakedTime = substr(trim($time),0,-3);
        $timeBits = explode(":", $nakedTime);
        $int = (int) $timeBits[0];
        if($int != 12 && $p == "PM")
        {
            $int += 12;
        }elseif($int == 12 && $p == "AM"){
            $int = "00";
        }else{
            $int = str_pad((string)$int,2,"0",STR_PAD_LEFT);    
        }
        $newTime = $int . $timeBits[1];
        //$newDateTimes[] = $dt->format('Ymd') . $newTime;
        //For cheking purposes...
        $newDateTimes[] = $time . " = " . $dt->format('Ymd') . $newTime;
    }
    return $newDateTimes;
}

echo "<pre>";print_r( getNewDateTimes($timeList, $startDate) );echo "</pre>";

Seems to work from the array supplied. You can try it as a standalone to see if it would be of any use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.