Getting PHP Date format correct - php

I can't seem to get this date format correct.
I'm looking to display the current date like this:
Wednesday, May 16th
Any help would be appreciated.
Thanks :)

echo date('l,M jS');
php manual
http://php.net/manual/en/function.date.php

If you want leading zeroes (e.g. "Wednesday, May 02nd"):
date("l, F sS");
If you do not want leading zeroes (e.g. "Wednesday, May 2nd"):
date("l, F jS");

Related

Convert time format in PHP

I am getting time in format
$cursor=new DateTime("now",new DateTimeZone("Australia/Melbourne"));
echo $cursor->format("h:i a");
So the output is 01:00 pm
And I need to convert the output to something like
13:00
How can I do this?
If you DO NOT want leading zero's...such as 1:00 instead of 01:00...
echo $cursor->format("G:i");
If you DO want leading zero's...
echo $cursor->format("H:i");
In any case, I recommend taking a peeksy at the documentation...
PHP Date Format
Wow you could have read the manual, use capital H. See the format section on that page
echo $cursor->format("H:i");
Fiddle
Use G:I format to do this. Use the code below
<?php
$cursor=new DateTime("now",new DateTimeZone("Australia/Melbourne"));
echo $cursor->format("G:i a");
Hope this helps you

RFC-822 DateTime Formatting in PHP with Database [duplicate]

This question already has answers here:
Conversion from MySQL date to RFC822 date format
(2 answers)
Closed 9 months ago.
I have a date field on my database. the date format as following.
June 17, 2013
Im using the format as
date("F j, Y");
So my question is there a way that i can display this date in RFC-822 format using php? or do i need to start saving the date in RFC-822 format from now on? Thanks in advance.
Using the following syntax, you can display current time in RFC822 Format.
$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
Neither.
From now on you have to start using format supplied by database.
You have to understand the difference between storage format and display formatting. It's different matters. When storing data in mysql, you have to follow mysql rules. So, instead of June 17, 2013 you have to store 2013-06-17.
And then convert at output to whatever format required - not limited to a single one but whatever format is demanded by destination.
None of the other answers worked for me, so this is what worked... to take a date in PHP and output it in RFC822:
date("D, d M Y G:i:s T", strtotime($date));
Hope that helps others.
As was pointed out your best bet is to change the way you are storing your dates to something other then a string. date("Y-m-d", strtotime($date)) can assist you in this endeavor.
But to solve the immediate need you can utilize use strtotime, date and the DATE_RFC822 constant to get you what you are looking for.
echo date(DATE_RFC822, strtotime($value));
See First example on php date documentation
As #ashleedawg and others mentioned in some comments the simplest solution that works:
date("D, d M Y H:i:s O", strtotime($date));
Mind the "H" and the "O" ;)
Thanks!
If you want to date format something in PHP for RFC-822 , then just do this...
date('r', strtotime($date))
'r' ยป RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
Source: PHP.net: DateFormat
But, as other stated, you don't want to store this in your database! However, you'll need to use r for other things, like XML-RSS date time formats...
All date-times in RSS conform to the Date and Time Specification of RFC 822... (Source: RSS 2.0 Specification.)
date_format(date(your database field), '%D, %j %M %t')
and what type of format you want just see the link
date and time format for Mysql
You can save it as TimeStamp in database and show it RFC822 format
date(DATE_RFC822, time());
This is the only solution that worked for me:
date("D, d M Y H:i:s T", strtotime($date));
Other examples above that didn't work include using the DATE_RFC822 format specifier, which puts out a 2-digit year, instead of 4 digits. Then the other suggestion to use G:i:s for time doesn't work because G specifies no leading zeroes, so you'll get 2:00:00 instead of 02:00:00.
don't use T at the end but an "O", it works for me

strtotime() always returns same day of month but gets other parts of date correct

I have a part of a simple web app that takes input from a JavaScript calendar picker, sends it to the server, and then the server converts it to a human readable time and echos it back out.
My HTML form ends up having a value formatted as MM/DD/YYYY.
When this gets POSTed to the server this PHP transforms it into a differet format (please note that I'm using CodeIgniter so $this->input->post() is the same as $_POST[]):
php
$date = date('l, F n, Y', strtotime($this->input->post('date')));
Example input and output
HTML text input will get a value of "04/21/2013".
PHP's strtotime() will echo back "Sunday, April 4, 2013".
No matter what date I put in there, strtotime() always gives me the correct date back except for the day of the month which always ends up being the same number as the number of the month (for example, any dates in May become "May 5, 2013" and so on).
Update: Solved
As soon as I posted this I realized it was the 'n' in 'l, F n, Y' that caused the issue. Turning it to a 'j' fixed things. Sorry to waste everyone's time.
Use j for day of the month, not n which is the numeric month:
php $date = date('l, F j, Y', strtotime($this->input->post('date')));
See it in action
See the documentation for date() here: http://php.net/manual/en/function.date.php
Change the n in the first parameter of your date function to j and you will get the number of the day of the month.

date function to display 7-12-2012 instead of 07-12-2012 & 01-01-2012 as 1-1-2012

the date function is displaying 07-12-2012 instead of 7-12-2012
I am using this date("Y-m-d H:i:s")
I want to display 7th December 2012 as 7-12-2012
what about displaying month as 1-1-2012 rather than 01-01-2012 for 1st Jan 2012
Use this instead:
date("Y-m-j H:i:s")
d shows the days with leading zeros, j just shows the days.
Edit: On second glance, the string you are using is totally different to the output you want:
date("j-m-Y");
Should get you what you want.
Use j instead of d.
echo date('j-m-Y');
Change to:
date("j-m-Y");
Read about variables here.

How can I get today's timestamp in PHP

I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.

Categories