Skip to content

Instantly share code, notes, and snippets.

View kosinix's full-sized avatar

Kosinix kosinix

  • Philippines
View GitHub Profile
@kosinix
kosinix / gist:5123613
Created March 9, 2013 09:27
Convert 12-hour time format with hour, minutes, seconds, and meridiem into 24-hour format. Performs data correction to make sure hours, minutes and seconds have leading zeros if needed. The trick here is to use strtotime() where we pass the time string in this format: "hh:mm:ss meridiem" Example: "02:30:00 pm"
<?php
function to_24_hour($hours, $minutes, $seconds, $meridiem){
$hours = sprintf('%02d',(int) $hours);
$minutes = sprintf('%02d',(int) $minutes);
$seconds = sprintf('%02d',(int) $seconds);
$meridiem = (strtolower($meridiem)=='am') ? 'am' : 'pm';
return date('H:i:s', strtotime("{$hours}:{$minutes}:{$seconds} {$meridiem}"));
}
echo to_24_hour( 1, 2, 3, 'pm' ); // Returns 13:02:03
@kosinix
kosinix / gist:5354637
Created April 10, 2013 13:30
Get month names using a for loop and PHP date function.
<?php
for($m=1; $m<=12; ++$m){
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}
@kosinix
kosinix / menu-css-class.php
Last active December 11, 2019 12:08
Add first and last CSS class to WordPress navigation menu items by using filters. CSS class will be added automatically without need for tinkering with WordPress admin. Note: Last class will be added to the last menu item. If the last menu item has sub menu, the last class will be added to the last item of that sub menu.
<?php
//Add extra css classes to menu items
function mytheme_nav_menu_css_class( $classes = array(), $item, $args ) {
$location_name = 'header';
if($args->theme_location== $location_name){//Limit to this menu location only
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $location_name ] ) ) {
$main_nav = wp_get_nav_menu_object( $locations[ $location_name ] );
@kosinix
kosinix / menu-css-class-top-level.php
Last active October 14, 2016 08:11
Add first and last CSS class only to TOP LEVEL navigation menu items by using filters. CSS class will be added automatically without the need for tinkering with WordPress admin.
<?php
//Add extra css classes to menu items
function mytheme_nav_menu_css_class( $classes = array(), $item, $args ) {
$location_name = 'header';
static $top_level_count = 0; //Top level menu items counter
if($args->theme_location== $location_name){//Limit to this menu location only
if($item->menu_item_parent==0 and $top_level_count!==null){ //Count top level menu items
$top_level_count++; //Increment
@kosinix
kosinix / list-posts-shortcode.php
Last active December 16, 2015 17:40
Shortcode for displaying list of posts with prev and next links. Place the code in functions.php and use it as follows: [list_posts] or in template files as a PHP code: <?php echo do_shortcode('[list_posts]'); ?>
<?php
function list_posts_shortcode(){
$my_query = new WP_Query(
array(
'post_type' => 'post',
'paged' => get_query_var('paged')
)
);
@kosinix
kosinix / media-gallery-custom.php
Last active April 27, 2017 17:37
Adds custom field to WordPress 3.5 Media Manager. The field's unique key is 'meta_link'.
@kosinix
kosinix / setter-getter-generator.php
Last active December 17, 2015 01:49
PHP class setter and getter generator. These functions can be added to a class to generate setter and getter functions for that class. Will save you a lot of typing when your class has many class variables.
<?php
/**
* Our example class
*/
class Person {
/**
* Class properties to generate the setter and getter functions
*/
protected $height;
@kosinix
kosinix / custom-nav-walker-usage.php
Last active April 25, 2023 09:32
WordPress: Using a custom nav walker to create navigation menus in plain <a> tags. That is the <ul> and <li> tags are not present. Very useful if you want to create simple links that can be centered with a simple text-align:center on the containing element.
<?php
// In your template files like footer.php
// The items_wrap value ('%3$s') removes the wrapping <ul>, while the custom walker (Nav_Footer_Walker) removes the <li>'s.
wp_nav_menu(array('items_wrap'=> '%3$s', 'walker' => new Nav_Footer_Walker(), 'container'=>false, 'menu_class' => '', 'theme_location'=>'footer', 'fallback_cb'=>false ));
?>
@kosinix
kosinix / get-first-array-element.php
Last active December 18, 2015 16:29
PHP - Simplest way to get first element of array
<?php
$arr = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' )
echo reset($arr); //echoes "apple"
@kosinix
kosinix / google-spreadsheet-reader.php
Last active December 18, 2015 18:49
Google spreadsheet reader in PHP
<?php
// URL to spreadsheet. Make sure to append ?alt=json to return contents in JSON format
$url = 'https://spreadsheets.google.com/feeds/list/0Ah0xU81penP1dFNLWk5YMW41dkcwa1JNQXk3YUJoOXc/od6/public/values?alt=json';
$file= file_get_contents($url);
$json = json_decode($file);
echo '<pre>'.print_r($json, 1).'</pre>';