Convert time format in PHP - 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

Related

Getting PHP Date format correct

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");

How to format php Datetime?

I am using this code to show last date of the month
<?php
$day=new DateTime('last day of this month');
echo $day->format('M jS');
?>
but its giving output as "Feb 28th"
I want output as "February 28th, 2017"
How do I achieve it?
thanks in advance
Php.net has great, understandable, documentation on every function. I really suggest reading it here, that is how I learned php. The format you're looking for is this:
echo $day->format('F jS, Y');
http://php.net/manual/de/function.date.php
This code outputs what you want.
echo date("F tS, Y",time());

php convert date to mysql format

I have gone through convert php date to mysql format but still I have few issues and questions,
In general, my application, can input dates in Y-m-d, d-m-y, m-d-Y, jS, F Y format, or even with '/' separator.
I want this dates to be converted in to MYSQL Y-m-d format,
I tried as below, but it shows different output than expected,
Non working
date_format(date_create_from_format('d-m-Y', '02-25-2016'), 'Y-m-d');
Working
date_format(date_create_from_format('d-m-Y', '25-02-2016'), 'Y-m-d');
So it seems format and string to be match in same way, otherwise its not interpreting correctly,
What is best way to convert above 4 format inputs to mysql(Y-m-d) format?
Thanks advanced,
I'd recommend using Carbon for any date processing in PHP these days.
Creating date by parsing strings is simple enough...
http://carbon.nesbot.com/docs/#api-instantiation
Getting the output in a format you need is also simple...
http://carbon.nesbot.com/docs/#api-formatting
I'd suggest you to use this function: DateTime::createFromFormat
Using this, you need to create a date by specifying the particular format.
Try this:
$date = DateTime::createFromFormat('Y-m-d', '2016-05-26');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('d-m-Y', '26-05-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('m-d-Y', '05-26-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('jS F Y', '26th May 2016');
echo $date->format('Y-m-d');
Thanks for all suggestions, i think i found my logic,
My date format selection is stored in sql db, so will try as below,
date_format(date_create_from_format('format from db', 'user entered format'), 'Y-m-d');
As user entered format is stored in db, so i will pull that in this date_format function, so both will match and i can convert to Y-m-d!
Thanks,

convert one date format to another

I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>

Could anyone pls convert this unixtime stamp in php?

I am struggling with this unixtimestamp and just cant find a correct format
Here is the stamp:
1295058844
And here is the result i want to achive:
01/14/2011 at 21:34 EST
And here is my almost correct but no luck code:
$start_unixtime = '1295058844';
date('m/d/Y \a\t H:i', intval($start_unixtime));
Basically i want EST time format, hope someone could help and sorry for such stupid question.
Thank you.
You need to use the date_default_timezone_set function before you call date.
date_default_timezone_set("America/New_York");
List of possible choices.
http://us2.php.net/manual/en/timezones.php
No need for intval.
date_default_timezone_set("TIMEZONENAME");
$start_unixtime = '1295058844';
echo date('m/d/Y \a\t H:i', $start_unixtime);
Maybe to get your "at" into there: date('m/d/Y',$x).' at '.date('H:i',$x)
After that I never could remember the codes, I tend to look them up every time.

Categories