How to format php Datetime? - php

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

Related

Convert date time to timestamp using php

I am quite sure this has been discussed before but for some reason mine is not working.
I am trying to convert date time to a timestamp.
echo strtotime("18 May 2.50pm");
The above code returns blank.
Any help is highly appreciated. Thanks in advance.
Use php DateTime::createFromFormat to make DateTime
<?php
$date = DateTime::createFromFormat('j M h.ia', '18 May 2.50pm');
echo $date->getTimestamp();
//print_r($date);
?>
Live Demo
Now you can use method available for DateTime .
Note : As you have not given year it will give you result for current year

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

PHP date_format TIME not diplayed correctly using Joomla

I'm having some problems displaying the TIME Only (H:i) in a row.
<td headers="qf_start_date" nowrap="nowrap"><?php echo date_format($course->start_date, '%H:%i'); ?></td>
This is the code, and as you can see, i want to see, under the start_date table in phpmyadmin, only Hours and minutes, nothing else. In the start_date query I want to have only, at the same time Y-m-d but i want to see in a page, only the hours and minutes...
I tried using date_format but it doesn't work...
What can i do?
I tried to include JDate but i think that i used a wrong sintax so i see blank page...
Thanks
To be more clear, visit: http://new.ivao.ch/index.php?option=com_seminarman&view=category&cid=4:rfe-geneva-departure&Itemid=227 and the table that i'm working in STD. It's blank...dunno why..
Thanks in advance
please followw at below :
<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>
or
<?php
$date = date_create('2000-01-01 12:15:25');
echo date_format($date, 'H:i:s');
?>
or
<?php
$valid_date = date( 'm/d/y g:i A', strtotime($date));
?>
or
<td headers="qf_start_date" nowrap="nowrap">
<?php
$valid_date = date( '%H:%i', strtotime($course->start_date));
echo $valid_date;
?>
Since it looks like you're working with Joomla, you can use the JDate class to help you.
If your $course->start_date is a valid date string (can be converted with strtotime, you may do the following):
// May fix class JDate not found errors. At the top of your script.
jimport( 'joomla.utilities.date' );
// ...
$date = new JDate($course->start_date)
echo $date->format('H:i');
Alternatively, (from the comments) it seems like your date string is not formatted as a UNIX timestamp. If you're not using Joomla, or do not want to use JDate you may use:
echo date('H:i', strtotime($course->start_date));
What's happening here is, for most date processing functions in PHP, you must pass it a valid UNIX timestamp.
A UNIX timestamp is measurement of the number of seconds since the Unix Epoch on January 1st, 1970 at UTC.
You can convert most date strings into one (including mysql DATETIME columns by passing it into php's strtotime() function.
From there, you pass it into date (or your desired date formatting function) with a desired format. Hope this helps!

Calculate a date with PHP

I am using an html form to get a user inputted date. The structure of the date inputted is: MM/DD/YYYY. I then need to increment the total days by 196 in PHP. Right now, the data is being posted to a php file called Calculate.php. I was looking into altering the data using date (m d Y); in php, but my friend said that probably wont work. Any ideas? Thank you for your time and have a great day! ^_^
Check out strtotime. This will convert your MM/DD/YYYY format in to a numeric value you can then work with.
Use strtotime again to manipulate the date to add the days to it.
Use strftime to re-format it for display.
e.g.
$d = '08/11/2011';
$dAsPOSIX = strtotime($d);
$dPlus196Days = strtotime('+196 day', $dAsPOSIX);
echo strftime('%m/%d/%Y',$dPlus196Days);
DEMO
Using strtotime magic:
strtotime('08/11/2011 +196 days');
$input = $_POST['date'];
echo date('m d Y', strtotime('+196 days', strtotime($input));

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