Convert PHP date format string to .NET DateTime format string
I recently had to convert some JavaScript code I'd written that used a PHP-style date format string to a .NET DateTime-style date format string.
It would've been a helluva lot faster to have had some sort of conversation table. After a little searching on Google, I found nothing so I decided to make one myself. Behold, the fruits of my labour!
To use a PHP date format string:
$time = date("g:i a")
To use a .NET date format string (in C#):
string time = DateTime.Now.ToString("h:mm tt")
Day | ||
---|---|---|
PHP date() | DateTime | Example |
d | dd | 01 to 31 |
D | ddd | Mon through Sun |
j | d | 1 to 31 |
l (lowercase L) | dddd | Sunday through Saturday |
N | - | 1 (for Monday) through 7 (for Sunday) |
S | - | st, nd, rd or th |
w | DateTime#DayOfWeek | 0 (for Sunday) through 6 (for Saturday) |
z | DateTime#DayOfYear - 1 | 0 through 365 |
Week | ||
---|---|---|
PHP date() | DateTime | Example |
W | - | 42 (the 42nd week in the year) |
Day | ||
---|---|---|
PHP date() | DateTime | Example |
F | MMMM | January through December |
m | MM | 01 through 12 |
M | MMM | Jan through Dec |
n | M | 1 through 12 |
t | DateTime#DaysInMonth() | 28 through 31 |
Year | ||
---|---|---|
PHP date() | DateTime | Example |
L | DateTime#IsLeapYear() | 1 if it is a leap year, 0 otherwise |
o | yyyy (?) | ISO-8601 year number |
Y | yyyy | 1999 or 2003 |
y | yy | 99 or 03 |
Time | ||
---|---|---|
PHP date() | DateTime | Example |
a | - | am or pm |
A | tt | AM or PM |
B | - | 000 through 999 |
g | h | 1 through 12 |
G | H | 0 through 23 |
h | hh | 01 through 12 |
H | HH | 00 through 23 |
i | mm | 00 to 59 |
s | ss | 00 through 59 |
u | fffff | 54321 |
Time Zone | ||
---|---|---|
PHP date() | DateTime | Example |
e | - | UTC, GMT, Atlantic/Azores |
I (capital i) | DateTime#IsDaylightSavingTime() | 1 if Daylight Saving Time, 0 otherwise |
O | - | +0200 |
P | zzz | +02:00 |
T | - | EST, MDT |
Z | - | -43200 through 50400 |
Full Date/Time | ||
---|---|---|
PHP date() | DateTime | Example |
c | - | 2004-02-12T15:19:21+00:00 |
r | - | Thu, 21 Dec 2000 16:01:07 +0200 |
U | - | Seconds since the Unix Epoch |
(Please note that just because I have no entry for the DateTime version of one of the PHP date strings, it doesn't mean you can't get that information using .NET easily. It just means that it's not in DateTime or not a one-liner.)
This table highlights some pretty interesting differences between the .NET API (i.e. a carefully designed API) and the PHP API (i.e. an ad-hoc API). For example, if I asked you what g
stood for in a PHP date format string, would you be able to say (without looking at a chart) "hours with no leading zeroes?"
What's cool about the .NET API is that they try to make the format strings mean something. For example, d
means "day number with no leading zeroes", dd
means "day number with leadings zeros", ddd
means "short form day name", dddd
means "long form day name". That is, as you add d's, you get progressive larger date string as a results.
1 comment