views:

20

answers:

1

I need to change the time part of a datetime in a database.

What I got is in the database: '2010-01-01 01:00:00' I need to update this to '2010-01-01 03:00:00' Only thing I have is '03:00:00'

As I'm using doctrine I could iterate through all objects but this would decrease the perfomance. So what I tried was:

$q = Doctrine_Query::create()
                ->update('Something s')
                ->set('start_at', 'DATEADD(DATESUB(start_at,HOUR_SECOND TIME(start_at)), HOUR_SECOND ?)', $doctrine_article->start_time)
                ->set('start_at', 'DATEADD(DATESUB(end_at,HOUR_SECOND TIME(end_at)), HOUR_SECOND ?)', $doctrine_article->end_time)
                ->where('s.some_id = ?',$doctrine_article->id)
                ->andWhere('s.start_at > ?',$current_date)
                ->execute();

(update)
To explain a bit more:
I tried to remove the time part of the current Datetime, so I get '2010-01-01 00:00:00' and then add to this the given time ('03:00:00') so at the end it should be the wanted '2010-01-01 03:00:00'

But this leads to an error (1064 You have an error in your SQL syntax)

Any hints how to solve this error and achieve the described functionality?

A: 

It would be easier to use the DATE function to remove the time part. After that, you can use different ways to add the time again:

start_at = DATE(start_at) + INTERVAL 3 HOUR

start_at = DATE(start_at) + INTERVAL 10800 SECOND

start_at = CONCAT(DATE(start_at), ' ', '03:00:00')

and so on.

Alexander Konstantinov
tried to do it this way: `->set('start_time', 'CONCAT(DATE(start_at), " ", ?', $doctrine_article->start_time)` But it resulted in a new error: `Call to undefined method Doctrine_Query_Set::parseLiteralValue()`
hering
First: you forgot closing bracket after `?`. Second: if that's won't be enough, try `->set('start_time', 'CONCAT(DATE(start_at), ?, ?)', array(' ', $doctrine_article->start_time))`
Alexander Konstantinov
Works like charme when executing as pure SQL. Within doctrine the same error occurs.
hering