Date(u) gives you the unix timestamp and so does strtotime. But your first date(u) the u needs to be in quotes which might be why it's not working.
Umm, field_releasedate_value should be a datestamp (if you have the date module installed, you can get that as a cck option) which would make everything easier for you, but if it's a string, you can work with that as well you just have to convert it.
Where is this code supposed to go? If it's for an individual node type, you should probably use template_preprocess_node in order to set variables and to do php logic.
<?php
function mythemename_template_preprocess_node(&$vars){
// $vars is where drupal keeps everything when processing a node.
// if you want to do this code for a specific node type, then check for that type
if (isset($vars['node']) && ($vars['node']->type == "mybandsnodetype")){
// gets the current UNIX timestamp
$today = time();
// strtotime will convert a string that looks like a date into unix timestamp
$release = strtotime($vars['node']->field_releasedate_value[0]['view']);
// how long do you want to display "outnow"?
$release_2weeks = strtotime("+14 days", $release);
// if release date is less than today, it's coming soon
if ($release > $today) {
$vars['soon'] = ' COMING SOON!';
}
// if release date +14 days is less than today, then print out now
// after 14 days don't print the out now
elseif ($release_2weeks < $today){
$vars['soon'] = ' OUT NOW!';
}
}
}
?>Then in your node template, you'll have a $soon variable to print out.
Umm, field_releasedate_value should be a datestamp (if you have the date module installed, you can get that as a cck option) which would make everything easier for you, but if it's a string, you can work with that as well you just have to convert it.
i set up the date module with a unix format, so in the view, i added the release date in a human readable form (to display on the page), and then the unix-formatted version for use here.
i had this code in (one of ) the view-view-fields.tpl.php files, along with all of the other styling information for that view, so that if anything printed from this if statement, i could wrap the whole thing in a div.
thanks for your reply. i'll do a little research on the template_preprocess_node you mentioned.
i have 2 really stupid questions.
when you say "Then in your node template, you'll have a $soon variable to print out," do you mean node.tpl.php? i ask this stupid question because i made a custom theme and it has no node.tpl.php
secondly, i'm putting this in my template.php file, right? i ask THIS stupid question because it isn't working.
Yeah, node.tpl.php, but if you've got a custom content type, you'll probably want a specialized template.
You can copy node.tpl.php from drupal/modules/node (this is the default template) to your theme folder. (If you do not have one in your theme's folder, drupal is using this one.)
To do a specialized template, if you're using cck and you called this content type, say, "disco" then you can create a node-disco.tpl.php in your theme folder (just copy node.tpl.php and modify it however you want) and drupal will automatically use that for putting out that specific content type.
Then inside node-disco-tpl.php you can simply write
<?php print $soon ?>And the stuff you did in preprocess will show up.
okey dokey. i must just be a total idiot. i've got
function library_template_preprocess_node(&$vars){
// $vars is where drupal keeps everything when processing a node.
// if you want to do this code for a specific node type, then check for that type
if (isset($vars['node']) && ($vars['node']->type == "record")){
// gets the current UNIX timestamp
$today = time();
// strtotime will convert a string that looks like a date into unix timestamp
$release = strtotime($vars['node']->field_releasedate_value[0]['view']);
// how long do you want to display "outnow"?
$release_2weeks = strtotime("+14 days", $release);
// if release date is less than today, it's coming soon
if ($release > $today) {
$vars['soon'] = ' COMING SOON!';
}
// if release date +14 days is less than today, then print out now
// after 14 days don't print the out now
elseif ($release_2weeks < $today){
$vars['soon'] = ' OUT NOW!';
}
}
}in my template.php file.
i've got
<?php print $soon; ?>in my node-record-tpl.php
and in my views-view-fields.tpl.php
i've got
<?php print $fields['field_releasedate_value']->content ?><div class="red"><?php print $soon; ?></div>and i'm still getting nothing. i feel so dumb right now. haha. i really thought i was getting a handle on this.
one thing i noticed, in the mustardseed podcast about creating variables, is he actually used the word "variables" instead of "vars," and i tried switching it, and that did nothing too.
When the preprocess function gets called, it's passed the variables array, when I write a preprocess function, I don't want to have to use $variables['node'] etc everywhere, so I shorten it to $vars or even just $v.
Your template file should be node-record.tpl.php not record-tpl. Oops, that's my bad, I mistyped earlier.
Are you getting any errors?
In the preprocess function add a print to see if soon is getting set:
<?php
// if release date +14 days is less than today, then print out now
// after 14 days don't print the out now
elseif ($release_2weeks < $today){
$vars['soon'] = ' OUT NOW!';
}
print $vars['soon'];
?>It'll appear at the top of your webpage.
ahhh. yes. i'm just an idiot. okay. soooo. NOW i have "OUT NOW!" in the top right corner of my site.
two things of odd note. i have the release date set for something ridiculous like 2013 or something. so it SHOULD say "coming soon!", and it's still not showing up in the view next to the release date.
is this because i've got it set up as a node view, rather than page?
Something isn't converting: what is the value of $release? (print it out in your preprocess function) That's probably where the problem is.
If you do a print_r on $vars['node']->field_releasedate_value it'll tell you what's in that array. Make sure $release is getting the correct value.
woah. that's crazy!
anyway. it returned this:
[field_releasedate] => Array ( [0] => Array ( [value] => 2010-09-14T00:00:00 [timezone] => America/New_York [timezone_db] => America/New_York [date_type] => date ) )i tried switching the line to
$release = strtotime($vars['node']->field_releasedate[0]['view']);Ah, see there isn't a "view" in the array. Use,
<?php
$release = strtotime($vars['node']->field_releasedate[0]['value']);
?>Normally, you don't want to use "value" without sanitizing it against someone putting something bad in it. But strtotime will sanitize it for us in this case because it'll only convert values that it can understand. So if someone were to put something weird in the field, strtotime won't do anything with it.
OKAY! YES! so up in the top right, it now correctly displays the "coming soon!" or "out now!" based on if i go in and change the date. and i'm able to print $release now and see a string.
BUT i'm still unable to print $soon in the view. i tried
<?php print $soon ?>
<?php print $vars['soon'] ?>LOL, okay we're getting closer.
If it's printing up in the top right, then the template is setting $soon. So you can remove those print statements if you want to.
Where are you trying to print $soon? You said in a view? It should be in your node-record.tpl.php.
essentially, here's what i've got.
i've a "record" node type, that is using a view with a node-reference URL so that the band can simply click "add a song" to add song titles (linked to the song node)...if you've watched the jeff eaton photo gallery video, i'm doing the exact same thing, but in this case the "gallery" is "record" and "photo" is "song." if that makes sense.
SO. there are 2 views set up for "record"
1. displaying the aforementioned song titles
2. displaying fields from the 'record' node itself; album art, title, format (cd/vinyl/digital download, etc), length (LP/EP/SINGLE), record label (if any), production credits, purchase information, and of course, release date.
here's what the views row template looks like
<?php
<div id="recordinfo_wrapper">
<div id="recordinfoimage"><?php print $fields['field_cover_image_fid']->content ?></div>
<div id="recordinfo">
<table class="recorddata">
<tr class="recorddata"><td><i>"<?php print $fields['title']->content ?>"</i></td></tr>
<tr><td><?php print $fields['field_format_value']->content ?>-<?php print $fields['field_format2_value']->content ?></td></tr>
<tr><td><?php print $fields['field_releasedate_value']->content ?><?php print $soon; ?></td></tr>
<tr><td><?php print $fields['field_record_label_url']->content ?></td></tr>
<tr><td><?php print $fields['field_producer_value']->content ?><?php print $fields['field_additionaltracks_value']->content ?><?php print $fields['field_producer2_value']->content ?>.</td></tr>
<tr><td>AVAILABLE FOR PURCHASE AT:
<br /><?php print $fields['field_purchase_url']->content ?></td></tr></table></div></div>
?>Ah, I thought you were using views to pull in nodes, but it looks like your using views to build the page so it's probably not going to use the node-.tpl.php file
If you've created your own views-view-fields.tpl.php file, then rename your preprocess function to "library_preprocess_views_view_fields__page(&$vars)"
(I think this is it, but download the devel module and enable the theming option. Then you can navagate to your page and it'll tell you what the function name should be.)
Here's a good tutorial: http://www.group42.ca/theming_views_2_the_basics
okay.
i THOUGHT i named it correctly.
my views template is:
views-view-fields--record--node-content-2.tpl
so i named my function
<?php
function library_preprocess_views_view_fields__record__node_content_2(&$vars){
?>it's weird. i've used the devel module for different things. here, it's displaying the "release date" as a date range, and still no $soon
okay. soooooooo. after bashing my head repeatedly on my desk, i noticed a few things.
1. if i delete the line
<?php if (isset($vars['node']) && ($vars['node']->type == "record")){ ?>it's days like this, i hate that i volunteered to do this site.
Man, it's really hard to debug something from the outside.
Since you have the devel module, in your preprocess function write dpm($var). That will give you everything in the variable array. When you find the release date variable, you can use that in your strtotime.
Also, since you're using a very specific preprocess function, you shouldn't need the if vars['node'] line.
$release = strtotime($vars['node']->field_releasedate_value[0]['view']);
is what i'm trying and failing. and i really am grateful for your help.
is there a good place i can read about this...for instance...why [0]? or how do you know do put ['view']
try as i might to find a good resource, i can't and i feel like a help vampire right about now.
lastly, i'm writing all my code in dreamweaver. is that stupid? like i mentioned, i'm totally new to this php mumbo jumbo. i just didn't know if there was something out there that would make for a little less trial and error?
okey dokey. so, there are two release date fields in this. the first is where the view displays the actual release date. the second was set up, using the date module custom format, to print unix (back when i was trying this waaaay before i even posted my initial post).
field_releasedate_value (Object) stdClass
-content (String, 59 characters ) September -26,...September 26, 2011
-raw (String, 19 characters ) 2011-09-26T00:00:00
-inline (Boolean) FALSE
-inline_html (String, 3 characters ) div
field_releasedate_value_1 (Object) stdClass
content (String, 51 characters ) 1317009600
raw (String, 19 characters ) 2011-09-26T00:00:00
inline (Boolean) FALSE
inline_html (String, 3 characters ) div
handler (Object) date_handler_field_multiple
*
∞ (Recursion)
element_type (String, 4 characters ) span
class (String, 25 characters ) field-releasedate-value-1
label (String, 0 characters )
Awesome, now we're cooking with fire. Personally, I'd use the timestamp one, but you can use either one since strtotime will understand both formats.
// In the first case you have to use the raw value
$release = strtotime($vars['field_releasedate_value']->raw);
// In the second case you can use the timestamp
$release = strtotime($vars['field_releasedate_value_1']->content);So pick which one you'd prefer.
For future reference, in php you use "->" to access objects and "[]" to access arrays. $vars is an array [] of objects that might have arrays inside them [].
For example you might run into array->object[array] (ie $vars['somearray_field']->someobject[0]['value'] )
argh.
okay. so. neither worked.
at the bottom of my preprocess, just to make sure SOMETHING was working, i put in
<?php
elseif ($release_2weeks < $today){
$vars['soon'] = ' OUT NOW!';
print $vars['soon'];
print $vars['field_releasedate_value_1']->content;
print $vars['soon'];
}
}
?>also, in my views tpl, i put in a print $release for good measure, and it isn't printing, yet the still incorrect "OUT NOW" is.
Yeah, if you're not printing the field_releasedate_value_1 then something still wrong.
When you did a dpm($vars) this is what you got? Or did you dpm something else?
field_releasedate_value_1 (Object) stdClass
content (String, 51 characters ) 1317009600
raw (String, 19 characters ) 2011-09-26T00:00:00
inline (Boolean) FALSE
inline_html (String, 3 characters ) div
handler (Object) date_handler_field_multiple
the dpm($vars) in this
<?php
drupal_rebuild_theme_registry();
/*
* Declare the available regions implemented by this engine.
*
* @return
* An array of regions. The first array element will be used as the default region for themes.
* Each array element takes the format: variable_name => t('human readable name')
*/
function library_preprocess_views_view_fields__record__node_content_2(&$vars){
dpm($vars);
// $vars is where drupal keeps everything when processing a node.
// gets the current UNIX timestamp
$today = time();
// strtotime will convert a string that looks like a date into unix timestamp
$release = strtotime($vars['field_releasedate_value_1']->content);
// how long do you want to display "outnow"?
$release_2weeks = strtotime("+14 days", $release);
// if release date is less than today, it's coming soon
if ($release > $today) {
$vars['soon'] = ' COMING SOON!';
}
// if release date +14 days is less than today, then print out now
// after 14 days don't print the out now
elseif ($release_2weeks < $today){
$vars['soon'] = ' OUT NOW!';
print $vars['soon'];
print $vars['field_releasedate_value_1']->content;
print $vars['soon'];
}
}
?>
spits out, for the second release instance,
field_releasedate_value_1 (Object) stdClass
content (String, 51 characters ) 1253246400
raw (String, 19 characters ) 2009-09-18T00:00:00
inline (Boolean) FALSE
inline_html (String, 3 characters ) div
handler (Object) date_handler_field_multiple
o
∞ (Recursion)
element_type (String, 4 characters ) span
class (String, 25 characters ) field-releasedate-value-1
label (String, 0 characters )
it occurs to me that maybe that span class is mucking up the process, yet even changing it to raw is not working.
there was no "node" object as a main dpm
but i found these that looked to be of interest
view (Object) view
build_info (Array, 5 elements)
query (String, 1817 characters )
o
SELECT node.nid AS nid,
node_data_field_cover_image.field_cover_image_fid AS
node_data_field_cover_image_field_cover_image_fid,
node_data_field_cover_image.field_cover_image_list AS
node_data_field_cover_image_field_cover_image_list,
node_data_field_cover_image.field_cover_image_data AS
node_data_field_cover_image_field_cover_image_data, node.type AS node_type,
node.vid AS node_vid, node.title AS node_title,
node_data_field_cover_image.field_format2_value AS
node_data_field_cover_image_field_format2_value,
node_data_field_cover_image.field_releasedate_value AS
node_data_field_cover_image_field_releasedate_value,
node_data_field_cover_image.field_record_label_url AS
node_data_field_cover_image_field_record_label_url,
node_data_field_cover_image.field_record_label_title AS
node_data_field_cover_image_field_record_label_title,
node_data_field_cover_image.field_record_label_attributes AS
node_data_field_cover_image_field_record_label_attributes,
node_data_field_cover_image.field_producer_value AS
node_data_field_cover_image_field_producer_value,
node_data_field_cover_image.field_additionaltracks_value AS
node_data_field_cover_image_field_additionaltracks_value,
node_data_field_cover_image.field_producer2_value AS
node_data_field_cover_image_field_producer2_value,
node_data_field_cover_image.field_purchase_url AS
node_data_field_cover_image_field_purchase_url,
node_data_field_cover_image.field_purchase_title AS
node_data_field_cover_image_field_purchase_title,
node_data_field_cover_image.field_purchase_attributes AS
node_data_field_cover_image_field_purchase_attributes FROM {node} node LEFT JOIN {content_type_record} node_data_field_cover_image ON node.vid = node_data_field_cover_image.vid WHERE (node.status <> 0) AND (node.nid = %d)as well as
view (Object) view
current_node (Object) stdClass
field_releasedate (Array, 1 element)
0 (Array, 4 elements)
value (String, 19 characters ) 2009-09-18T00:00:00
timezone (String, 16 characters ) America/New_York
timezone_db (String, 16 characters ) America/New_York
date_type (String, 4 characters ) date | (Callback) date();Umm, is that view->field_releasedate or is field_releasedate it's own entry?
So if you do a
<php
print $vars['view']->field_releasedate[0]['value']; // OR
print $vars['field_releasedate'][0]['value'];
?>what do you get? You should be able to stick that in the strtotime function...
it was view (object)->current node (object)->field_releasedate (array)
i tried
print $vars['field_releasedate'][0]['value'];earlier, based on some of your previous info about objects and arrays (i learned something!), but it didnt work
and just tried
print $vars['view']->field_releasedate[0]['value'];let me go back through this thing and see if i can find ANYTHING else.
view (Object) -> result (Array, 1 element) -> 0 (Object) -> node_data_field_cover_image_field_releasedate_value (String, 19 characters ) 2009-09-18T00:00:00
view (Object) -> result (Array, 1 element) -> 0 (Object) -> date_info (Object) -> id (String, 25 characters ) field_releasedate_value_1
view (Object) -> result (Array, 1 element) -> 0 (Object) -> date_info (Object) -> table (String, 27 characters ) node_data_field_releasedate
view (Object) -> result (Array, 1 element) -> 0 (Object) -> date_info (Object) -> field (String, 23 characters ) field_releasedate_value
view (Object) -> result (Array, 1 element) -> 0 (Object) -> aliases (Array, 4 elements) -> field_releasedate_value (String, 51 characters )
view (Object) -> display (Array, 3 elements) -> node_content_2 (Object) views_display -> display_options (Array, 5 elements) -> fields (Array, 11 elements) -> field_releasedate_value (Array, 14 elements) -> field_releasedate_value (Array, 14 elements)
everything else goes PRETTY deep. i can always dig deeper or attach a file or something with the whole enchilada, but it's like 7000 lines
Ah, okay, this was the dpm($vars) right?
If so then the value we're looking for should be
<?php
$release = strtotime($vars['fields']['field_releasedate_value']->raw);
// do some prints just to make sure everything is happy
print "<p>my date is: " . $vars['fields']['field_releasedate_value']->raw . "</p>";
print "<p>my release: " . $release . "</p>";
?>okay! after messing with this for a bit, i figured it out (the "OUT NOW" wasn't working)
<?php
function library_preprocess_views_view_fields__record__node_content_2(&$vars){
dpm($vars);
// $vars is where drupal keeps everything when processing a node.
// gets the current UNIX timestamp
$today = time();
$break = ' ';
// strtotime will convert a string that looks like a date into unix timestamp
$release = strtotime($vars['fields']['field_releasedate_value']->raw);
// how long do you want to display "outnow"?
$release_2weeks = strtotime("+14 days", $release);
// if release date is less than today, it's coming soon
if ($release > $today) {
$vars['soon'] = ' COMING SOON!';
}
// if release date +14 days is less than today, then print out now
// after 14 days don't print the out now
elseif (($release < $today) && ($today < $release_2weeks)) {
$vars['soon'] = ' OUT NOW!';
}
}
?>thank you so much for all of your help! i would, so i can learn, love to know how you realized that $release = strtotime($vars['fields']['field_releasedate_value']->raw); was the right thing to use.
Be sure to remove your debug print statements (when your page loads you see "OUT NOW OUT NOW" flash at the top. ;)
How do you know what to use? Well, you got the dsm($vars) working (which is really the hard part: knowing that your function is getting used by drupal) so that tells you everything that you have available to work with. The fields array is handy but you could also find the same information inside the views array (but the views array is HUGE and you'd have to really dig inside that to find the right value).
Good luck with your site!
i'll to be honest, i wasn't QUITE sure if i should put this in the "drupal" forum here, or the php, but since it seems more a php thing than a strictly drupal thing, i figured i'd post it here.
okay. so i've been trying SO hard not to hop on a forum every single time something comes up, because figuring stuff out for yourself is learning. but once again, i come with a question that has me stumped.
so as i've mentioned in earlier topics, i'm working on a band website.
they have a discography page. for each album, they input a release date.
they have a new record coming out in a few months. it got me thinking, that if i could compare the local date to the date input for that record's node, before it comes out, i could print "coming soon," until it comes out, and then "out now!" for a period of a few weeks.
right now i'm working on the "coming soon" bit, and here's what i've got so far.
<?php$today = date(U);
$release = $fields['field_releasedate_value_1']->content;
$unixrelease = date("U", strtotime($release));
$soon = 'COMING SOON!';
$space = ' ';
if ($unixrelease > $today) {
print $space;
print $soon;
} else {
print $space;
}
?>
i was thinking that unix epoch, which for those that dont know essentially converts a date/time into a string of seconds that have passed since a particular date in 1970, would be the smarter way to compare these two numbers because a date for today would be of a lesser numerical value than a date for tomorrow, so in my head if the record came out tomorrow, today's number is less than tomorrows, and thusly it would be labeled "coming soon."
in views, i have "field_releasedate_value_1" spitting out a unix string. and i can, for instance, say "print $release" and it will print said unix epoch string. AND i can print $today and get a unix epoch string. BUT trying to print $unixrelease isnt working, nor is trying to compare the two.