PHP Logo

Comparing Dates in PHP

If you are a web developer sometimes we have to know multiple languages. At my job, I have to know PHP, ASP.NET, JavaScript, jQuery, HTML, CSS, SQL. You get the point. Even though programming logic is pretty similar across the board, the actual syntax of the languages are different. For example, in PHP you can declare a variable as such:

$carModel = 'Nova';

In ASP.NET,to declare the same variable we have to assign a data type:

string carModel = 'Nova';

In JavaScript, to declare the same variable:

var carModel = 'Nova';

So you can see that the logic is pretty much the same, just the syntax is different. So let us take a look at comparing dates in PHP. Dealing with Dates in all of the different languages are the same. So when I had to do some PHP coding for a WordPress plugin dealing with dates, I had to reference the PHP website. So whenever I come across issues that I have to spend time to research, I try to write a post and pass along my findings. Hopefully, this will save some people some time. I do not want to offend daily PHP developers. This guys that work with PHP on a daily basis can write this code in seconds with their eyes closed. But a lot of web developers, like me, have to know multiple languages. So here is how to work with dates in PHP and deal with comparing dates.

So first I had to get the current date, using PHP’s date function:

$todaysDate = date("m/d/y");

What this does is gets today’s date and formats it as such (01/31/2014). You can format the date in many different ways. See above link to the PHP date function page to see all the different ways of formatting. Then I have another variable that was assigned another date value from the database:

$car_end_date = $custom["car_end_date"][0];

Then I do an if statement and using PHP srttotime function, I can compare the dates. The PHP srttotime function “Parses about any English textual datetime description into a Unix timestamp”. So running both date variables though these function now we have both dates formatted exactly the same. We can now compare Apples to Apples instead of comparing my formatted date to whatever format date I got back from the database. That is it. Here is my code segment to see how it works. I hope this post has helped someone and saved them some time Goggling and searching the internet.

$car_end_date = $custom["car_end_date"][0];
$todaysDate = date("m/d/y");
if (strtotime($car_end_date) > strtotime($todaysDate))
{
     echo 'Yes';
}
else
{
     echo 'No';
}

 

Blog, PHP, Web Development,
WordPress Logo

Adding Dashicons to Custom Post Types Admin Menu

OK, WordPress Designers and Developers. I finally have figured out how to use the new Dashicons. If you do not know what the Dashicons are, go to the website here. With WordPress getting a nice overhaul in the admin theme, Dashicons are used throughout the new admin theme. So I just got finished making a new plugin which required a custom post type. So I wanted to give the new post type a cool new Dashicon then the default pin. So here are the steps:

1. In the custom post type script where you normally state the menu_icon this is what your syntax will be (see highlighted line # 22):

function create_orl_books()
{
register_post_type( 'orl_books',
  array(
    'labels' => array(
    'name' => 'ORL books',
    'singular_name' => 'ORL Book',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New ORL Book',
    'edit' => 'Edit',
    'edit_item' => 'Edit ORL Book',
    'new_item' => 'New ORL Book',
    'view' => 'View',
    'view_item' => 'View ORL Book',
    'search_items' => 'Search ORL Book',
    'not_found' => 'No ORL Book found',
    'not_found_in_trash' =>
    'No ORL Book found in Trash'
    ),
    'public' => true,
    'menu_position' => 15,
    'menu_icon' => 'dashicons-[book]',    'supports' => array('title', 'editor', 'thumbnail')
    )
  );
}

2. OK. Now we know where to place the code correctly, how do we get the Dashicons? Go to the Dashicons website and find the icon that you would like to use.Click on the icon and go back to the top of the Dashicon home page and click on copy html. There you will see what the CSS class is and get that name and place it in the brackets on the menu-icon line.

3. Save your css page and then refresh your admin webpage and this is what you should see:

Blog, Web Design, Web Development, WordPress,