Home

PHP, JS, C#, SQL, and MySQL Time Conversions

Since this is Programming Times I figure that maybe the search index is throwing some people to my Knowledge Base so I am going to below discuss how to format time in common software languages. I will start with the most Common which is PHP.

PHP Date Time Conversion

The first thing with PHP no matter the string you have you need to convert into a system readable format to convert many different ways. When I started I commonly came across the issue of trying to throw a string value like this

<?php
$var = "20160212";
echo $var->format('Y-m-d');
?>

And Does this Work? The answer is not you will get a exception error every time.

What you will soon learn is that the string needs to be converted into a date object for the Php function to understand. But in some cases it is not as simple as that.

<?php
$var = "20160212";
//THIS IS THE DATE TIME OBJECT
$date = new DateTime($var);
//SO THIS SHOULD RETURN A ITEM THAT CAN BE FORMATTED. NO.
echo $date->format('Y-m-d H:i:s');
?>

What happens above it that we in fact have provided a string that can not be converted to a DateTime object because the string needs some kind of delimiter.

<?php
$var = "2013-01-08";
$parseit = date("m-d-Y", strtotime($var));
//WOULD WORK
//AND SO WOULD THIS
$date = new DateTime($var);
echo $date->format('Y-m-d H:i:s');
?>

To solve the issue of having a date conversion issues in this instance the string with no delimiter is the problem and the solution is to format it first.

Codes For Conversion Key

<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>

JS Date Time Conversion

Like Php we still need to change our string to a version that the Date Object can understand.

var date_str = "20160212";
//WILL NOT WORK WE MUST MAKE IT
var date_str = "2016-02-12";
var formatted_date = new Date(date_str);
//OTHER VARIOUS DATE FORMATS
var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);


C# Date Time Conversion

Written by | Milo Thompson

With 5 years of complex coding and implementation experience to go along with Networking Solutions I have Created many Different Software for Many different Platforms. I am the Owner of Jimnio.com, Former Developer for Magnaflow, San Francisco Health Plan, Eburgeon, Software Master, Wendal Wilson Business and Technology Consulting, S&S Tire

Google+ linkedin

1 Comment

  1. Milo Thompson

    January 13, 2017 at 2:33 PM

    milooollolo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll To Top