Wordpress : Display Text While a period - php

I have a question.
How can display a text while 4 days in PHP ?
Example : I wrote a post today, and I want display "New" while 4 days and hide this after that.
Thanks a lot.

This is NOT specific to WordPress, and you'll want to remove the foreach loop for actual use.. but it should give you an idea of how to compare relative dates:
<?php
$sample_dates = array(
'2011-02-17',
'2011-02-18',
'2011-02-19',
'2011-02-20',
'2011-02-21',
'2011-02-22',
'2011-02-23'
);
$post_date = '2011-02-17';
foreach ($sample_dates as $date_today) {
echo '<p>If today is ' . $date_today . ', the post from ' . $post_date . ' is: ';
if (strtotime('+4 days',strtotime($post_date)) >= strtotime($date_today)) {
echo 'NEW';
} else {
echo 'OLD';
}
echo '</p>' . PHP_EOL;
}
?>

Related

Instagram API pagination in PHP

I am trying to create a small instagram app in PHP only (no database) and without getting an access_token (just my client_id). So far so good, (i.e. input user_id returns photos from last 30 days, with likes-count and created_time, in a table), until I get to pagination. As expected, I want to hit a 'more' button which loads next json file and adds additional photos to the existing table, but it falls apart there... Here is what I've got, working, except for the pagination attempt.
NOTE: this is an internal app, so the sensitivity of my client_id is not an issue, if it is exposed
<?php
if (!empty($_GET['user_id'])){
$user_id = ($_GET['user_id']);
$instagram_url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent/?client_id=MY_CLIENT_ID';
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json, true);
}
?>
...
<?php
if(!empty($instagram_array)){
$instagram_array['pagination'] as $page { // Attempt at pagination
echo '<p>' .$page['next_url'].'</p>'; // Attempt at pagination
} // Attempt at pagination
foreach($instagram_array['data'] as $image){
if ($image['created_time'] > strtotime('-30 days')) {
echo '<tr>';
echo '<td>' . date('M d, Y', $image['created_time']) . '</td>';
echo '<td>'.$image['likes']['count'].'</td>';
echo '<td><img src="'.$image['images']['standard_resolution']['url'].'" alt=""/ style="max-height:40px"></td>';
echo '</tr>';
}
}
}
?>
</body>
</html>
Note: this is cobbled together from a few other sources - I am a total noob, so please forgive me if I need a little hand-holding...:)
You may specify min_timestamp to return medias which taken later than this timestamp
https://api.instagram.com/v1/users/{user_id}/media/recent/?access_token={access_token}&min_timestamp={min_timestamp}
$instagram_array['pagination']['next_url'] should be removed, it may include your access token which is a sensible data, that must be always invisible.
list_ig.php
<?
$user_id = "...";
$access_token = "...";
//30 day ago
$min_timestamp = strtotime("-30 day",time());
//pagination feature
$next_max_id = $_GET['next_max_id'];
$instagram_url = "https://api.instagram.com/v1/users/" . $user_id . "/media/recent/?access_token=" .$access_token. "&min_timestamp=" . $min_timestamp;
if($next_max_id != "")
$instagram_url .= "&max_id=" . $next_max_id;
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json ,true);
?>
<? if( $instagram_array['pagination']['next_max_id'] != "" ): ?>
More
<? endif;?>
.... print instagram data....
Instagram AJAX Demo
http://jsfiddle.net/ajhtLgzc/

Google Analytics API: How to get all Report ID

I want to select all the Report ID of all the added URLs in the Google Analytics account.
How can I do something like this?
SELECT report_id FROM google_analytics_account
Example URLs in the Google Analytics account:
https://www.google.com/analytics/web/?hl=en#home/a3456789w2345678p1234567/
https://www.google.com/analytics/web/?hl=en#home/a3456789w2345678p9876543/
Where the Report IDs there are 1234567 and 9876543.
Can I do that using the GAPI class only? How?
I have a gapi class.
<!-- language: lang-php -->
require_once 'gapi.class.php';
$ga = new gapi('my_test_email#gmail.com', 'my_test_email_password');
$report_id = 1234567;
$dimensions = array('browser', 'browserVersion');
$metrics = array('pageviews', 'visits');
$sort_metric = null;
$filter = null;
$start_date = null;
$end_date = null;
$start_index = 1;
$max_results = 30;
$ga->requestReportData(
$report_id,
$dimensions,
$metrics,
$sort_metric,
$filter,
$start_date,
$end_date,
$start_index,
$max_results
);
foreach ($ga->getResults() as $result) {
echo '<strong>'.$result.'</strong><br />';
echo 'Pageviews: ' . $result->getPageviews() . ' ';
echo 'Visits: ' . $result->getVisits() . '<br />';
}
echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';
echo '<font color="blue"><pre>',print_r($ga->getResults()),'</pre></font>';
echo '<hr />';
Thanks! :)
You can add a filter to your request. something like this should do
ga:landingPagePath%3D#ReportID=1234567,ga:pagePath%3D#ReportID=9876543
I'm not sure if its ga:pagePath or ga:landingPagePath that you will need to use check your google analytic's see which page it is you where looking at. Its probably ga:landingPagePath you want.
Also the , is an OR seproator so if you want to add more of them just add another with a ,.

PHP Conditional Field Display Joomla

Basically I need to call a field (wrapped in a div) if the field has a value. I do not want the field or div to display if the field has no value. My PHP knowledge is dire but here is what I have to work with.
Here are instructions provided to me on how to call a custom field by ID:
$cust_1 = $this->fields->getFieldByCaption('Custom Text'); // getFieldByCaption() allow you to get the field by the Caption. This is not the best way to get a field since changing the caption in the back-end will break the reference.
echo '<br />Field ID: ' . $cust_1->getId();
$cust_2 = $this->fields->getFieldById(29); // getFieldById() is the ideal way of getting a field. The ID can be found at 'Custom Fields' section in Mosets Tree's back-end.
echo '<br />Name: ' . $cust_2->getName();
echo '<br />Has Caption? ' . (($cust_2->hasCaption()) ? 'Yes' : 'No');
echo '<br />Caption: ' . $cust_1->getCaption();
echo '<br />Value: ' . $cust_2->getValue();
echo '<br />Output: ' . $cust_2->getOutput(1);
echo '<hr />';
$this->fields->resetPointer();
while( $this->fields->hasNext() ) {
$field = $this->fields->getField();
echo '<br /><strong>' . $field->getCaption() . '</strong>';
echo ': ';
echo $field->getOutput(1); // getOutput() returns the formatted value of the field. ie: For a youtube video, the youtube player will be loaded
// echo $field->getValue(); // getValue() returns the raw value without additional formatting. ie: When getting value from a Online Video field type, it will return the URL.
$this->fields->next();
}
Here is the code I am working with and need help to try and do what I need it to as detailed above:
<?php
if( !is_null($this->fields->getFieldById(35)) ) {
$value = $field->getValue();
$hasValue = $field->hasValue();
$field = $this->fields->getField();
if( $hasValue )
{
echo '<div class="lin" id="lphn">'; {
echo $cust_2 = $this->fields->getFieldById(35);
echo $cust_2->getOutput(1);
echo '</div>';
}
else {
// Print nothing.
}
}
}
?>

wordpress category name in shortcode php

i want to display the current category name in code below in my sidebar but i cant. Can anyone help me?
Thank you.
<?php
$weather_city = the_category_name();
if ($weather_city)
echo do_shortcode('[forecast location="' . $weather_city . ', Greece" caption="" measurement="C" todaylabel="Σήμερα" datelabel="%%weekday%%" highlow="%%high%%° / %%low%%°" numdays="4" iconset="Incredible" class="css_table_class"]'); ?>
$current_categories = get_the_category(); // array of objects
if (!empty($current_categories)) {
$category = reset($current_categories); // take the first one
$weather_city = $category->cat_name;
echo do_shortcode('[forecast location="' . $weather_city . ', Greece" caption="" measurement="C" todaylabel="Σήμερα" datelabel="%%weekday%%" highlow="%%high%%° / %%low%%°" numdays="4" iconset="Incredible" class="css_table_class"]');
}

PHP/MySQL show last updated/most recent date

I have a mysql query that brings in a list of results, say 10, each with its own 'last edited' date, and was wondering if it was possible to display/echo/print ONLY the most recent of these dates using php?
Now I know I could do this with a mysql query, but I need to display all of the available results, but only 1 date, (the most recent).
Hope this makes sense. Any help greatly appreciated. S.
<?php
$availQuery=mysql_query("select * from isavailability");
$availResult=mysql_fetch_array($availQuery);
$date = $availResult['editDate'];
$newDate = date('D dS F Y',strtotime($date));
$time = date('G:i:s',strtotime($date));
echo '<p>This page was last updated on '.$newDate.' at '.$time.'</p>' . PHP_EOL;
while($availR=mysql_fetch_array($availQuery)) {
echo '<tr class="rows">'. PHP_EOL;
echo '<td><p>'.$availR['title'].'</p></td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==1) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==2) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==3) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '</tr>'. PHP_EOL;
}
?>
It can be done in a single query: SELECT isavailabilit.*, MAX(editDate) AS LastEditDate FROM isavailabilit;
Use it this way in the loop: $availR['LastEditDate']
If your query is ordered by your timestamp then you could just grab the first one - if I understand your question:
$myDate = $myData[0]['myDateColumn'];
foreach($myData as $row)
{
echo $myDate . ' ' . $row['someotherColumn'] . "\n";
}
or something similar? Presuming your results are in an array.

Categories