• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Working with dates in PHP and JavaScript
Home
JavaScript

Working with dates in PHP and JavaScript

May 4th, 2025 Ivan Dimov JavaScript, PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

PHP

You can use the date function to generate a date string of your desired format. As a second parameter, you can add a UNIX timestamp representing the date that you wish the string to hold.

Examples:

echo date("Y-m-d");

The above example will print a date of the format year(4 digits)-month-day of the month. The full set of formatting options can be seen at: http://php.net/manual/en/function.date.php

echo date("Y-m-d h:i:s", time() - 86400);

The above example would print yesterday’s date in the format year (4 digits)-month-day of the month hours:minutes:seconds. It will print a date in the format 2017-02-02 09:44:43.

You can use the strtotime function to convert a date string to a unix timestamp: For example, strtotime(“2017-12-22”); would return a unix timestamp in the format 1513929600,

It is also possible to use strftime to get a string representing certain date in exchange for a UNIX timestamp.

Example:

echo strftime("day %A",time());

Strftime uses a different format to embed the date counterparts in the string and they can be viewed as usual in the PHP documentation: http://php.net/manual/en/function.strftime.php

You can use the time() function to get a UNIX timestamp of the current time in the server’s locale.

JavaScript

In JavaScript there are a handful of methods that can help you get the date.

new Date() a detailed string ot the current date. +new Date() will get you a timestamp of the current date. You can also get the constituent parts of the current date by chaining methods.

Examples:

new Date().getMonth() // 1 (the month will be between 0 and 11, not 1 to 12



new Date()..getDate() // 6



new Date().getFullYear() // 2017



new Date().getHours() // a number between 0 and 23 with the date’s hour



new Date().getSeconds() // the seconds part of the date - from 0 to 59



new Date().getDay() // the day of the week - 0 for Sunday, 1 for Monday and so on until 6 for Saturday

You can actually pass a date to the date object and then get the constituent parts of that date and work with it: new Date(‘December 25, 2017 23:15:30’).getMonth() // 11

Also, you can change the date dynamically using the provided setters.

date = new Date()



Mon Feb 06 2017 04:07:15 GMT+0200 (FLE Standard Time)



date.setMonth(11)



1512526035261



date



Wed Dec 06 2017 04:07:15 GMT+0200 (FLE Standard Time)

You can set the month (setMonth), the date (setDate), the hours (setHours), the year (setFullYear) with those analogously named methods.

More information on the Date object can be seen at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article Useful Laravel functions and methods, Part-1
Previous article The Art of Debugging in Software Development: A Comprehensive Guide

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

Web Scraping With PHP - Easy Step-By-Step Guide API
June 16th, 2025

Web Scraping With PHP - Easy Step-By-Step Guide

HTML based swipe Tabs for mobile / touch devices CSS
June 16th, 2025

HTML based swipe Tabs for mobile / touch devices

PHP Beyond 2023: Unfurling the Road Less Traveled PHP
June 15th, 2025

PHP Beyond 2023: Unfurling the Road Less Traveled

Leave a Reply Cancel reply

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

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Free PHP, HTML, CSS, JavaScript/TypeScript editor – CodeLobster IDE
  • Web Scraping With PHP – Easy Step-By-Step Guide
  • HTML based swipe Tabs for mobile / touch devices
  • Cool HTM5 Features, Part 2
  • Programming Languages for Better Job Opportunities
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.