Skip to main content

Date to select

More
11 years 5 months ago #1494 by MichielStr
Date to select was created by MichielStr
I am trying to write a date to Select8 (menu_8) using a callback-function. The function ends with the following line
Code:
$item['menu_8'] = $curDate->format('Y-m-d H:i:s');

When running the import script, everything seems to be OK:
Code:
[menu_8] => 2012-10-27 00:00:00

Nothing is written to the database-field, however.

I have tried changing the properties of the field to DATETIME, to no avail. Is the feedsql.php doing some magic, preventing me from doing what I want?

Anybody got any ideas?

Please Log in to join the conversation.

More
11 years 5 months ago #1497 by redactie
Replied by redactie on topic Re: Date to select
The importer will remove the - and :

But frills here it looks fine, how does the complete function look like?

Please Log in to join the conversation.

More
11 years 5 months ago - 11 years 5 months ago #1498 by MichielStr
Replied by MichielStr on topic Re: Date to select
A short explanation of what I am accomplishing with the code below. The code allows me to calculate the expiry time and date of a 'daily offer'. Only a time (hh:mm) is sent with the feeds. So I assume that if the time provided is smaller than the current time, the expiry date is actually tomorrow. Quite safe, since I'm dealing with daily offers (not lasting more than 24 hours). So if it currently is 13:30 and the time supplied is 24:00, the deal expires at 24:00 today. If, however, the time supplied is 12:00, the deal probably expires tomorrow at 12:00...
Code:
function daytime(&$item){ if ((substr($item['menu_8'], 0, 2) - date('H')) <= 0) { //Only time (hh:mm) is provided, no date / if hour provided smaller than current hour, then assume enddate is tomorrow $curDate = new DateTime(); //get current date and time $curDate->setTime(0, 0, 0); //set time to zero, just want the date for now $extraTime = substr($item['menu_8'], 0, 2); //get hours $curDate->add(new DateInterval('P1DT'.$extraTime.'H')); //add hours to tomorrow's (1D) date (to get correct expiry time) $item['menu_8'] = $curDate->format('Y-m-d H:i:s'); //pass expiry time to menu_8 } else { //expiry date is today $curDate = new DateTime(); $curDate->setTime(0, 0, 0); $extraTime = substr($item['menu_8'], 0, 2); $curDate->add(new DateInterval('PT'.$extraTime.'H')); //add hours to today's date $item['menu_8'] = $curDate->format('Y-m-d H:i:s'); } } function daytime_cb(&$item) { generic_cb($item); daytime($item); }
Last edit: 11 years 5 months ago by MichielStr.

Please Log in to join the conversation.

More
11 years 5 months ago #1504 by redactie
Replied by redactie on topic Re: Date to select
code doesn't work so I can't give it a dry run.

with php you can actually easy find 'today' and 'tomorrow' reducing your problem to comparing the hour and creating strings
Code:
<?php function time2date($time) { list($h,$m)=explode(':',$time); if ( $h && $h!=24) { $nu=date('H', time()); if ( $h <= $nu ) { $timestamp = strtotime("tomorrow"); } else { $timestamp = strtotime("today"); } $res=date('Y-m-d'.' '.$h.':00', $timestamp); } else { #uur is nul dus morgen eigen vandaag 23:59 $timestamp = strtotime("tomorrow"); $res=date('Y-m-d'.' 00:00', $timestamp); #$timestamp = strtotime("today"); #$res=date('Y-m-d'.' 23:59', $timestamp); } return $res; } #test foreach (array('10:00','12:00','00:00','24:00') as $v ) { print $v . ' ' . time2date($v). "\n"; } $item['menu_8']='24:00'; $item['menu_8']=time2date($item['menu_8']); print $item['menu_8'] . "\n";

Please Log in to join the conversation.

More
11 years 5 months ago #1505 by MichielStr
Replied by MichielStr on topic Re: Date to select
No idea why the code doesn't work at your end. It does at mine.

Anyhow, how does your solution solve my problem of not being able to write a DATETIME to one of select fields?

And I need to do the calculation, just retrieving the today or tomorrow date doesn't work with what I am trying to accomplish.

Please Log in to join the conversation.

More
11 years 5 months ago #1507 by MichielStr
Replied by MichielStr on topic Re: Date to select
I have solved it by by-passing the prepare_for_sql_url function in feedsql.php in case of a date. Probably not ideal, but it works.

Please Log in to join the conversation.

Time to create page: 0.337 seconds