uly.me

cloud engineer

  • Home
  • Category
    • Cloud
    • PHP
    • Linux
    • HTML
    • WP
    • CSS
    • SVN
    • Mac
    • Git
    • JS
    • Misc
  • About
    • Me
    • Archives
    • Contact
    • Portfolio
    • Podcasts
  • Code
    • Github
    • Docker Hub
    • Unfuddle
You are here: Home / PHP / Comparing Time

May 9, 2017

Comparing Time

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

Subscribe

Cloud

  • Extend an EBS Volume
  • Convert JSON to YAML
  • AWS S3 Killedted
  • Serverless Jekyll on AWS
  • AWS and VMWare

Linux

  • Cronjob Not Running
  • Password Expiry
  • Multiple VirtualHosts
  • Convert JSON to YAML
  • Empty A File

PHP

  • Comparing Time
  • Quick Database Check
  • Laravel 5.4 Install
  • Mass Find and Replace
  • WordPress Mobile Detect

Copyright © 2018. All rights reserved.