0

I was wondering how can I turn the following date into an array.

PHP code.

$current_date = date('Y-m-d H:i:s'); //current date

I want to put the date in a loop to compare it to other dates.

4
  • 1
    Elaborate. What should the components of the array be? Commented Dec 15, 2010 at 7:08
  • And just for curiosity's sake, what for? Commented Dec 15, 2010 at 7:09
  • to put the date in a loop to compare it to other dates Commented Dec 15, 2010 at 7:10
  • 2
    An array with a single date and a loop is the same as a single date and no loop. What's the goal? Commented Dec 15, 2010 at 7:11

4 Answers 4

2
$current_date = array(date('Y-m-d H:i:s'));
Sign up to request clarification or add additional context in comments.

Comments

1

The built-in php function strptime() converts a date to an array. See the linked documentation for details about the structure of the array it produces.

Comments

0

I think you want to get the components of the function return into an array.

// Method one

$current_date = array(date('Y'),date('m'),date('d'),date('H'),date('i'),date('s'));

// Method two

$current_date = date('Y-m-d H:i:s'); //current date
$exploded_current_date = explode(" ", $current_date);
$date = explode("-",$exploded_current_date[0]);
$time = explode(":",$exploded_current_date[1]);
$current_date = array_merge($date,$time);

Update:

// Method three

$current_date = getdate();

/*
Returns 
Array
(
    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520
)
*/

Comments

0

Here is an example.

<?php
$format = '%d/%m/%Y %H:%M:%S';
$strf = strftime($format);
echo "$strf\n";
print_r(strptime($strf, $format));
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.