David Wampamba

2 minutes read

Trouble comparing dates in PHP

The scenario.
I’m building a custom tool which automates the curation of blogs from member bloggers’ feeds. These blogs must be fetched on a weekly basis because the intention is to make sure every member blogger, blogs at least once a week.
Where to the feed URLs come from?
Member joins by submitting a blog feed URL, which will save in the system manually at the moment.
The system(bot) must;

  1. Retrieve the feed URLs from the database.
  2. The system should loop the URLS.
  3. Connect to each URL.
  4. Fetch one feed item(latest).
  5. Get the feed item date and compare it to the date of last Sunday. If the feed date is greater than the date of last Sunday.
  6. Connect to the database again and retrieve a blog item whose item title is similar to the feed item title. If a blog item similar to the feed item is returned, skip that feed item since we already have it and move to the next. Repeating steps 2 to 6.
  7. If the blog item similar to the feed item is not returned, insert it in our blog database to store it permanently.

Tools used.
First of all, I chose PHP programming language to accomplish the task. I would have chosen Python, however, my hosting environment doesn’t have python. Yet am also more experienced in PHP than Python.
I also hard to extend WordPress so that I don’t have to build everything from scratch. Using WordPress will give me the opportunity to automate member sign up, log in and feed submission easier than if I used something different.
I will eventually build a frontend interface for the system. This interface will enable the public readers to choose and read any blog of their choice as well as enable bloggers to sign up from any part of the world. Building a WordPress theme will be much easier than if I built everything from scratch.
Alternative tools one could look at Joomla, Drupal and Django and many others.
What have I built so far?
So far I have built a WordPress plugin, with the following features.

  1. Automated Feed fetcher
  2. Shortcode which lists active bloggers. This is helpful for displaying member bloggers on the homepage or any page. Hence make it easy for readers to navigate blogs of any member(author)
  3. Shortcode for listing blogs. The intention of the system is to curate blogs from consistent bloggers around the world and make them accessible by consistent readers. This shortcode helps in displaying the blogs on the platform.

Challenges

  1. Comparing dates.

PHP has several inbuilt date functions and objects such as DataTime(), CreateDate() etc but they were not helpful. Sometimes they could function but they were never consistent. Luckily enough converting the date strings to time saved me after many trials and struggle.

Solution

$TimelastSunday = strtotime(‘last Sunday’);

$TimeFeedItem = strtotime($feedItem->get_date(‘m-j-Y’));

if($TimeFeedItem > $TimeLastSunday) {

// Do the rest

}

Whoa! Whoa!
Can’t wait to enable automated signing up, blog submission and attachment of categories and tags to the fetched/retrieved feeds.
Thanks for reading until next time.

Sharing is caring

Facebook
Twitter
LinkedIn
WhatsApp
Email

You may also like this

You deserve to be there at the top.

I’m glad to present about “deserving to be there at the top” at the first WordCamp in Africa in 2023. This is a wonderful opportunity to connect and share my experience with the community. #WordCampEbbs happening now at the Uganda

Read More »

When not to use a forEach loop in JavaScript

A forEach loop is one of the most optimized JavaScript Higher-Order functions that make it one of the preferred functional programming languages. What is a Higher-Order function? It is a function which takes in another function (also called a callback)

Read More »
Resources

You are not about to die poor.

In recent years, the biggest number of people I have met are characterized by the following;- Lazy Not ready Entitled Ignorant Dishonest Inconsistent It sounds unfortunate, that most probably you will meet people of similar characteristics or more if you’ve

Read More »
Resources

Thoughts on 13-06-2019

I promised to share my thoughts in any possible way. In fact, am happy to be honoring you, my readers, that today by sharing something that could be life changing to some. Please, I request you to converse through the

Read More »
Verified by MonsterInsights