Оcenka-BEL.com - безплатни онлайн тестове, пробни изпити, видео уроци и помощ при писане на задания от литературно и хуманитарно естество

Working with dates in PHP and JavaScript

Публикувано: 2018-02-16 09:32:59

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:

 

1

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

 

1

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:

 

1

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:

 

1

2

3

4

5

6

7

8

9

10

11

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.

 

1

2

3

4

5

6

7

8

9

10

11

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

 

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

-->