TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: MySQL FROM_UNIXTIME() Function
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

MySQL FROM_UNIXTIME() Function

Last updated: Feb 24, 2024 11:00 pm
By Meenakshi Agarwal
Share
4 Min Read
MySQL FROM_UNIXTIME() Function with Examples
MySQL FROM_UNIXTIME() Function with Examples
SHARE

This tutorial explains how to use the MySQL FROM_UNIXTIME() function with the help of examples. By using it, you can convert or display a UNIX_TIMESTAMP value to a timestamp.

Contents
SyntaxExamplesCalling FROM_UNIXTIME() in string contextPassing UNIX_TIMESTAMP() as input to FROM_UNIXTIME()Calling FROM_UNIXTIME() in numeric contextUsing FROM_UNIXTIME() format argumentPrint hour from MySQL FROM_UNIXTIME() outputSummary – MySQL FROM_UNIXTIME()

The output format is either YYYY-MM-DD-HH-MM-SS or YYYYMMDDHHMMSS, which depends on the calling context of the function. It means that FROM_UNIXTIME() would return a string for a string and a number in a numeric operation.

Please note that MySQL also has this function UNIX_TIMESTAMP(). It gives us a value in seconds since ‘1970-01-01 00:00:00’ UTC as an unsigned number. We are going to use the result of this function in our examples.

MySQL FROM_UNIXTIME()

This function provides a date/datetime formatted in a Unix timestamp style. The return value represents the number of seconds elapsed since January 1, 1970, GMT, with 12:00:01 (EPOC TIME).

Syntax

It has the following template:

-- MySQL From_UNIXTIME functions
FROM_UNIXTIME(UNIX_TIMESTAMP, [FORMAT]);

The UNIX_TIMESTAMP parameter is a UNIX timestamp value. The FORMAT is an optional argument, and it describes the format of the output.

Let’s now go through some of the examples using the MySQL FROM_UNIXTIME() function.

We think that the following topics would even help more along with this tutorial:

  • MySQL TIMESTAMP
  • MySQL Date and Date Functions

Examples

Calling FROM_UNIXTIME() in string context

In this example, we are passing plain EPOC time value in the string context.

-- Numeric context
SELECT FROM_UNIXTIME(99);
SELECT FROM_UNIXTIME(1441563925);
SELECT FROM_UNIXTIME(1565043000);

The above MySQL statements would produce the date in standard string format. Check below:

-- Output
1970-01-01 00:01:39
2015-09-06 18:25:25
2019-08-05 22:10:00

Passing UNIX_TIMESTAMP() as input to FROM_UNIXTIME()

Let us check out what happens when we use the return value of UNIX_TIMESTAMP() to pass as a parameter. Here, we like to demonstrate how one function handles the output of the other.

-- Calling one function as parameter of other
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP("2019-08-07"));
SELECT UNIX_TIMESTAMP("2019-08-07"), FROM_UNIXTIME(1565136000);
SELECT UNIX_TIMESTAMP(CURDATE()), FROM_UNIXTIME(1565198329);

The above MySQL statements would produce the date in standard string format. Check below:

-- Output
2019-08-07 00:00:00
1565136000 2019-08-07 00:00:00
1565136000 2019-08-07 17:18:49

Calling FROM_UNIXTIME() in numeric context

In this example, we are passing plain EPOC time value in the string as well as in numeric context.

-- Compare string vs. numeric output
SELECT FROM_UNIXTIME(1565136000);
SELECT FROM_UNIXTIME(1565136000) + 0;

The output is as follows:

-- Output
2019-08-07 00:00:00
20190807000000

Using FROM_UNIXTIME() format argument

Now, we’ll fill up the second argument of FROM_UNIXTIME() function. And try to customize the output format.

-- Printing a formatted output using FROM_UNIXTIME()
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');

The result of this command is as follows:

-- Output
2019 7th August 05:25:21 2019

Print hour from MySQL FROM_UNIXTIME() output

We’ll use DATE_FORMAT() along with FROM_UNIXTIME() to determine the format of the hour.

-- Print hour
SELECT DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP()), '%H');

The result after execution is:

-- Output
17

Summary – MySQL FROM_UNIXTIME()

We hope you should now feel comfortable in using the MySQL FROM_UNIXTIME() function. However, you can take up more examples and practice.

Also, to learn SQL from scratch to depth, do read our step by step MySQL tutorial.

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

Simple Bash Scripting Tutorial for Beginners

The Difference between UPSERT & Insert

SQL Programming Test in 2024

TAGGED:Unix Commands and Concepts
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python to Find Difference Between Two Lists Python to Find Difference Between Two Lists
Next Article How to Grant Privileges in MySQL Grant Privileges on a Database in MySQL

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x