Jump to content

[SOLVED] Date in an array issue


ainoy31

Recommended Posts

Hello-

 

I have an array with the following data:

Array
(
    [0] => Array
        (
            [0] => Del Date/Time:01/31/0811:20 AM
        )

    [1] => Array
        (
            [0] => 01/31/0811:20 AM
        )
)

$var = $array[1][0];  //this gives me 01/31/0811:20 AM
$date = substr($var, 0, 11); //this gives me 01/31/08
$deliv_date = date("Y-m-d", strtotime($date)); //this suppose to give me 2008-01-31

 

My problem is the last piece of code: $deliv_date = date("Y-m-d", strtotime($date)); 

It returns a date of 1969-12-31 but I need it to return 2008-01-31.  I echo $date and it is 01/31/08.  Much appreciation.

 

Link to comment
https://forums.phpfreaks.com/topic/89380-solved-date-in-an-array-issue/
Share on other sites

code:

<?php
  $array = array(
    array('Del Date/Time:01/31/0811:20 AM'),
    array('01/31/0811:20 AM')
  );
  $var = $array[1][0];
  $date = substr($var, 0, ;
  $deliv_date = date("Y-m-d", strtotime($date));
  echo $deliv_date;
?>

output:

2008-01-31

 

What am I missing?

print each variable to see where the problem is:

 

<?php
  $array = array(
    array('Del Date/Time:01/31/0811:20 AM'),
    array('01/31/0811:20 AM')
  );
  print_r($array);
  $var = $array[1][0];
  print "\$var: $var\n";
  $date = substr($var, 0, ;
  print "\$date: $date\n";
  $deliv_date = date("Y-m-d", strtotime($date));
  print "\$deliv_date: $deliv_date\n";
?>

should produce:

Array
(
    [0] => Array
        (
            [0] => Del Date/Time:01/31/0811:20 AM
        )

    [1] => Array
        (
            [0] => 01/31/0811:20 AM
        )

)
$var: 01/31/0811:20 AM
$date: 01/31/08
$deliv_date: 2008-01-31

Yeah.  I figured out my issue.  I am screen scraping data and what was getting me is the html tags.  I went and viewed the source code and the array is as follows:

[1] => Array

        (

            [0] => <b>01/31/08<BR>11:20 AM</b>

        )

I had to use the following code:

 

echo $var =  trim(strip_tags($array[1][0]));
echo "<br>";
echo $d = substr($var, 0, ;
echo "<br>";
echo $deliv_date = date("Y-m-d", strtotime($d));

 

This gave me what I am needing.  Thanks for the help.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.