Php date to formatted string

Php date to formatted string

  • Different ways to write a PHP code
  • How to write comments in PHP ?
  • Introduction to Codeignitor (PHP)
  • How to echo HTML in PHP ?
  • Error handling in PHP
  • How to show All Errors in PHP ?
  • How to Start and Stop a Timer in PHP ?
  • How to create default function parameter in PHP?
  • How to check if mod_rewrite is enabled in PHP ?
  • Web Scraping in PHP Using Simple HTML DOM Parser
  • How to pass form variables from one page to other page in PHP ?
  • How to display logged in user information in PHP ?
  • How to find out where a function is defined using PHP ?
  • How to Get $_POST from multiple check-boxes ?
  • How to Secure hash and salt for PHP passwords ?
  • Program to Insert new item in array on any position in PHP
  • PHP append one array to another
  • How to delete an Element From an Array in PHP ?
  • How to print all the values of an array in PHP ?
  • How to perform Array Delete by Value Not Key in PHP ?
  • Removing Array Element and Re-Indexing in PHP
  • How to count all array elements in PHP ?
  • How to insert an item at the beginning of an array in PHP ?
  • PHP Check if two arrays contain same elements
  • Merge two arrays keeping original keys in PHP
  • PHP program to find the maximum and the minimum in array
  • How to check a key exists in an array in PHP ?
  • PHP | Second most frequent element in an array
  • Sort array of objects by object fields in PHP
  • PHP | Sort array of strings in natural and standard orders
  • How to pass PHP Variables by reference ?
  • How to format Phone Numbers in PHP ?
  • How to use php serialize() and unserialize() Function
  • Implementing callback in PHP
  • PHP | Merging two or more arrays using array_merge()
  • PHP program to print an arithmetic progression series using inbuilt functions
  • How to prevent SQL Injection in PHP ?
  • How to extract the user name from the email ID using PHP ?
  • How to count rows in MySQL table in PHP ?
  • How to parse a CSV File in PHP ?
  • How to generate simple random password from a given string using PHP ?
  • How to upload images in MySQL using PHP PDO ?
  • How to check foreach Loop Key Value in PHP ?
  • How to properly Format a Number With Leading Zeros in PHP ?
  • How to get a File Extension in PHP ?
  • How to get the current Date and Time in PHP ?
  • PHP program to change date format
  • How to convert DateTime to String using PHP ?
  • How to get Time Difference in Minutes in PHP ?
  • Return all dates between two dates in an array in PHP
  • Sort an array of dates in PHP
  • How to get the time of the last modification of the current page in PHP?
  • How to convert a Date into Timestamp using PHP ?
  • How to add 24 hours to a unix timestamp in php?
  • Sort a multidimensional array by date element in PHP
  • Convert timestamp to readable date/time in PHP
  • PHP | Number of week days between two dates
  • PHP | Converting string to Date and DateTime
  • How to get last day of a month from date in PHP ?
  • PHP | Change strings in an array to uppercase
  • How to convert first character of all the words uppercase using PHP ?
  • How to get the last character of a string in PHP ?
  • How to convert uppercase string to lowercase using PHP ?
  • How to extract Numbers From a String in PHP ?
  • How to replace String in PHP ?
  • How to Encrypt and Decrypt a PHP String ?
  • How to display string values within a table using PHP ?
  • How to write Multi-Line Strings in PHP ?
  • How to check if a String Contains a Substring in PHP ?
  • How to append a string in PHP ?
  • How to remove white spaces only beginning/end of a string using PHP ?
  • How to Remove Special Character from String in PHP ?
  • How to create a string by joining the array elements using PHP ?
  • How to prepend a string in PHP ?
Читайте также:  HTML ESP8266

Источник

date_format

The format of the outputted date string . See the formatting options below. There are also several predefined date constants that may be used instead, so for example DATE_RSS contains the format string ‘D, d M Y H:i:s’ .

The following characters are recognized in the format parameter string

format character Description Example returned values
Day
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l (lowercase ‘L’) A full textual representation of the day of the week Sunday through Saturday
N ISO 8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st , nd , rd or th . Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week
W ISO 8601 week number of year, weeks starting on Monday Example: 42 (the 42nd week in the year)
Month
F A full textual representation of a month, such as January or March January through December
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year
L Whether it’s a leap year 1 if it is a leap year, 0 otherwise.
o ISO 8601 week-numbering year. This has the same value as Y , except that if the ISO week number ( W ) belongs to the previous or next year, that year is used instead. Examples: 1999 or 2003
X An expanded full numeric representation of a year, at least 4 digits, with — for years BCE, and + for years CE. Examples: -0055 , +0787 , +1999 , +10191
x An expanded full numeric representation if requried, or a standard full numeral representation if possible (like Y ). At least four digits. Years BCE are prefixed with a — . Years beyond (and including) 10000 are prefixed by a + . Examples: -0055 , 0787 , 1999 , +10191
Y A full numeric representation of a year, at least 4 digits, with — for years BCE. Examples: -0055 , 0787 , 1999 , 2003 , 10191
y A two digit representation of a year Examples: 99 or 03
Time
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds with leading zeros 00 through 59
u Microseconds. Note that date() will always generate 000000 since it takes an int parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. Example: 654321
v Milliseconds. Same note applies as for u . Example: 654
Timezone
e Timezone identifier Examples: UTC , GMT , Atlantic/Azores
I (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise.
O Difference to Greenwich time (GMT) without colon between hours and minutes Example: +0200
P Difference to Greenwich time (GMT) with colon between hours and minutes Example: +02:00
p The same as P , but returns Z instead of +00:00 (available as of PHP 8.0.0) Examples: Z or +02:00
T Timezone abbreviation, if known; otherwise the GMT offset. Examples: EST , MDT , +05
Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400
Full Date/Time
c ISO 8601 date 2004-02-12T15:19:21+00:00
r » RFC 2822/» RFC 5322 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See also time()
Читайте также:  Html iframe css width

Unrecognized characters in the format string will be printed as-is. The Z format will always return 0 when using gmdate() .

Note:

Since this function only accepts int timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create() .

Return Values

Returns the formatted date string on success.

Источник

How to convert DateTime to String in PHP

How to convert DateTime to String in PHP

In this article, we will learn how to convert a Date or a DateTime into a String in PHP.

The Format method from DateTime

To convert a Date or a DateTime object into a String in PHP, we can use the format method from the DateTime class. As the name already says, the format method can help us formatting a DateTime object into multiple different formats.

To do so, we simply need to specify the desired format as an argument when calling the format method. That’s it!

In the following section, we will see a practical example of how to use the format method to return a string according to the given format. This method is supported from PHP 5 >= 5.2.1 to the latest version.

DateTime to String example

Let’s now take a look at a practical example. In the code below, we will use format to simply convert a DateTime object into different String formats:

format( 'd/m/Y, H:i:s' ); echo 'datetime: ' . $date_time . '
'; $date = $current_datetime->format( 'd/m/Y' ); echo 'date: ' . $date . '
'; $time = $current_datetime->format( 'H:i:s' ); echo 'time: ' . $time . '
'; $day = $current_datetime->format( 'd' ); echo 'day: ' . $day . '
'; $month = $current_datetime->format( 'm' ); echo 'month: ' . $month . '
'; $year = $current_datetime->format( 'Y' ); echo 'year: ' . $year . '
';

In the example below, we used the DateTime class to initialize the $current_datetime variable with a new DateTime object and using a string. Subsequently, we used the format method to convert the DateTime object into the following formats:

  • DateTime: using the ‘d/m/Y, H:i:s’ pattern
  • Date: using the ‘d/m/Y’ pattern
  • Time: using the ‘H:i:s’ pattern
  • Day: using the ‘d’ pattern
  • Month: using the ‘m’ pattern
  • Year: using the ‘Y’ pattern.

If we run the code below, the output is:

datetime: 17/04/2021, 17:29:15 date: 17/04/2021 time: 17:29:15 day: 17 month: 04 year: 2021

External resources:

Источник

Оцените статью