• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

date

Calculate Date to Epoch Time

June 24, 2022

Here’s a script to convert date to epoch time on Mac OS.

#!/bin/bash
if [[ -z $1 ]]; then
  echo "Format: date2epoch.sh 2022/01/01-04:58:06"
else
  echo "$1"; date -j -f date -j -f "%Y/%m/%d-%T" "$1" +"%s"
fi

#!/bin/bash if [[ -z $1 ]]; then echo "Format: date2epoch.sh 2022/01/01-04:58:06" else echo "$1"; date -j -f date -j -f "%Y/%m/%d-%T" "$1" +"%s" fi

Result

$ ./date2epoch.sh 2022/08/31-22:00:00
2022/08/31-22:00:00
1661997600

$ ./date2epoch.sh 2022/08/31-22:00:00 2022/08/31-22:00:00 1661997600

Filed Under: Linux Tagged With: conversion, date, epoch

Set Linux Time Zone

January 27, 2020

Here’s another way of setting Linux time zone.

Check server time.

# check server time
date

# check server time date

Change to local time.

# set to local time
rm /etc/localtime
ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

# set to local time rm /etc/localtime ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

Check server time again.

# check server time again
date

# check server time again date

Filed Under: Linux Tagged With: date, localtime, set, time, zone

Calculate Epoch Time to Date

June 27, 2019

Here’s how to calculate epoch time using the Linux date command.

# Linux
date --date @1561266781
date -d @1561266781
# MacOS
date -r 1561266781

# Linux date --date @1561266781 date -d @1561266781 # MacOS date -r 1561266781

Output is : Sun Jun 23 05:13:01 UTC 2019

Show current epoch time.

date +%s

date +%s

Output: 1561642643

Filed Under: Cloud Tagged With: calculate, date, epoch, time

Add date to filenames

June 23, 2019

Here’s how to add date stamps to filenames. Uses epoch time.

tar cpzf mybackup-`date +%s`.gz /home/user

tar cpzf mybackup-`date +%s`.gz /home/user

Filed Under: Linux Tagged With: date, filenames, tar

A month ago

March 19, 2019

How to get last month’s value. Useful with Bash scripts.

LASTMONTH = $(date +%Y%m -d '1 month ago')
echo $LASTMONTH
# Output is ...
201902

LASTMONTH = $(date +%Y%m -d '1 month ago') echo $LASTMONTH # Output is ... 201902

Filed Under: Linux Tagged With: bash, date, month, scripts

Comparing Time

May 9, 2017

Here’s a neat little PHP script that compares a set date/time to the current time. The time() function sets the $now variable to the current Unix timestamp. The strtotime() function converts a date string to a Unix timestamp. The result is assigned to the $setdate variable. The two variables containing Unix timestamps are then compared. A simple if-then statement determines if the date is in the past or future.

date_default_timezone_set('America/Los_Angeles');
$setdate = strtotime("May 9, 2017 12:27PM");
$now = time();
if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

date_default_timezone_set('America/Los_Angeles'); $setdate = strtotime("May 9, 2017 12:27PM"); $now = time(); if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

You may have to change the default timezone if your server is in a different timezone.

Filed Under: PHP Tagged With: comparison, date, time, unix timestamp

Ubuntu Server Timezone

April 24, 2017

If you want to run a couple of cron jobs, you may need to check if your server is using your timezone. You can determine the server’s timezone by simply typing the system’s date command. You’ll see right away if your server is using the same timezone.

$ date
Sun Apr 23 21:21:24 PDT 2017

$ date Sun Apr 23 21:21:24 PDT 2017

If it isn’t, then you need to change it so it’s in sync with cron jobs. To change timezone, run dpkg-reconfigure tzdata. You’ll be asked to choose a region and the nearest city. In this example, I’m picking the Americas/Los Angeles combination. Choose your own region and city.

$ sudo dpkg-reconfigure tzdata
Current default time zone: 'America/Los_Angeles'
Local time is now:      Sun Apr 23 21:24:57 PDT 2017.
Universal Time is now:  Mon Apr 24 04:24:57 UTC 2017.

$ sudo dpkg-reconfigure tzdata Current default time zone: 'America/Los_Angeles' Local time is now: Sun Apr 23 21:24:57 PDT 2017. Universal Time is now: Mon Apr 24 04:24:57 UTC 2017.

You may need to restart cron so that the new timezone can take effect.

$ sudo service cron restart

$ sudo service cron restart

Filed Under: Linux Tagged With: date, timezone

Counting Down The Days

November 18, 2015

Today, I would like to share with you how to create a WordPress countdown plugin of a future date and time. The plugin output displays the number of days, hours, minutes and seconds left. The plugin uses a WordPress shortcode that you can insert inside your post. A Shortcode makes it easier to display results in WordPress.

Output: [counting_down_the_days]

Here’s the plugin code:

/*
Plugin Name: Counting Down the Days
Plugin URI: http://uly.me
Description: php countdown
Version: 1.0
Author: Ulysses Ronquillo
Author URI: http://uly.me
*/
 
// place your php code here
 
function urr_countdown_days() { 
 
date_default_timezone_set('Americas/New_York');
$date = strtotime("January 27, 2017 7:15 PM");
$remaining         = $date - time();
$days_remaining    = floor($remaining / 86400);
$hours_remaining   = floor(($remaining % 86400) / 3600);
$minutes_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))/60);
$seconds_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))-($minutes_remaining*60));
 
return 'There are '.$days_remaining.' days and '.$hours_remaining.' hours and '.$minutes_remaining.' minutes and '.$seconds_remaining.' seconds left.';
 
}
add_shortcode('counting_down_the_days','urr_countdown_days');

/* Plugin Name: Counting Down the Days Plugin URI: http://uly.me Description: php countdown Version: 1.0 Author: Ulysses Ronquillo Author URI: http://uly.me */ // place your php code here function urr_countdown_days() { date_default_timezone_set('Americas/New_York'); $date = strtotime("January 27, 2017 7:15 PM"); $remaining = $date - time(); $days_remaining = floor($remaining / 86400); $hours_remaining = floor(($remaining % 86400) / 3600); $minutes_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))/60); $seconds_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))-($minutes_remaining*60)); return 'There are '.$days_remaining.' days and '.$hours_remaining.' hours and '.$minutes_remaining.' minutes and '.$seconds_remaining.' seconds left.'; } add_shortcode('counting_down_the_days','urr_countdown_days');

The PHP date function is used to calculate the days, hours, minutes and seconds remaining and returns the output. The script is one of many PHP examples online on how to perform countdowns. This was one of the more popular ones. The script also uses the PHP floor function to get the lowest integer while ignoring the leftover fraction or remainder. To use the plugin, place the shortcode ‘counting_down_the_days’ (wrap it in square brackets) anywhere inside your WordPress post.

Output: [counting_down_the_days]

If you set a date in the past, the output will say, “Set a future date!”

Filed Under: PHP Tagged With: date, php, plugin, wordpress

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023