Gunnit
Gunnit

Reputation: 1074

Add increasing time to a date (php)

Hello everybody and thanks for taking the time to read this ;

I was wandering if I have a variable that contains a formatted date like this :

07/02/2012

That I obtained with the following query

date_format(data,'%d/%m/%Y') as data_scadenza,

How can I loop this date to have something like this:

2012-02-07 11:53:00, 2012-02-07 11:54:00, 2012-02-07 11:55:00, etc.

Thanks everybody in advance, was wandering how to do this either in PHP or directly in the MySQL query.

Upvotes: 0

Views: 54

Answers (2)

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

Loop through it in PHP.

$num = 5; //5 values will be printed
$phpdate = strtotime('07/02/2012'); //converts to timestamp
$phpdate += 11*3600 + 53*60; //to set initial offset to 11:53:00
for($i=0;$i<$num;$i++)
echo date( 'Y-m-d H:i:s', $phpdate + 60*$i).","; 

Output:

2012-07-02 11:53:00,2012-07-02 11:54:00,2012-07-02 11:55:00,2012-07-02 11:56:00,2012-07-02 11:57:00,

Upvotes: 2

Madhivanan
Madhivanan

Reputation: 13700

Have a number table with values from 1 to 100 and use this

select date_format(data+interval number minute,'%d/%m/%Y') as data_scadenza your_table as t1
cross join number_table as num 

Upvotes: 0

Related Questions