i want month name from date function using php - php

date_default_timezone_set('Asia/Calcutta');
$date=date("d:m:Y");
Now `$date=14072015;
I want month name 'July' from this date function. i have tried this,but i got output January.
$arr1=explode(":",$date);
$arr2=implode($arr1);
// print_r($arr2);die;
// print_r($arr1);die;
// print_r($date);DIE;
$year=date("Y");
$month=date("M",strtotime($arr2));
print_r($month);DIE;
I Want OUTPUT July.Please help.if anyother methods to solve this please help.

string date ( string $format [, int $timestamp = time() ] )
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
Read php.net manual

Try
<?php echo date('F', time());?>
In your question you are using $month=date("M",strtotime($arr2));.
Use time($arr2) instead of strtotime($arr2)
DEMO

"F" returns the full name of month
<?php echo date('F'); ?>

You could use date function directly in php
try something like this
echo date("F");

U can Try this below function
date('F');

Related

Substruct days stored in variable from date in PHP

As the title says I am trying to subtract a number of days (stored in variable) from a date. I am using the date_sub function.
This is the variable which contains the days that I want to substract (integer from a field)
$days=$rowRate['days'];
The function:
$chargedate=date_sub($arrivaldate,date_interval_create_from_date_string($days."days"));
$arrivaldate is the variable that contains the date that i want to substract the $days from.
When I use it, I get the warning date_sub() expects parameter 1 to be DateTime, string given in :\xampp\htdocs\Project\file.php on line 55.
I think I am messing up with the concatenation but I can not figure this out. Any help would be appreciated.
Try converting the $arrivaldate variable like this before passing it into the date_sub function:
$arrivaldate = date('Y-M-D', strtotime($arrivaldate));
$chargedate=date_sub(
$arrivaldate,
date_interval_create_from_date_string($days."days")
);
Per the error message you need to create a DateTime object from the $arrivaldate before calling date_sub:
$date = date_create(date('Y-m-d', strtotime($arrivaldate)));
$chargedate = date_sub($date,date_interval_create_from_date_string($days." days"));
echo date_format($chargedate, "Y-m-d");
I added the strtotime because it's not clear what format you are using but it may not be necessary.
The date_sub and date_add functions are only used in practice if a DateInteval is already available. Your task can be solved more efficiently and easily with DateTime::modify().
$arrivaldate = "2021-08-16";
$days = "5"; //may be also integer
$startDate = date_create($arrivaldate)->modify(-$days."Days");
//test output: 11/08/2021
echo $startDate->format("d/m/Y");
Important: The date in $arrivaldate must have a format wich accepted from DateTime.

Format DATETIME from MYSQL database using PHP

So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action

how to display 4-1 as month and date in php

I Am developing a php web application. Here I have date in a variable in the format 'm/d'
$var='04/25'
I have stored this value as strtotime with in my table
$var2=strtotime($var);
Then I need to display this value as 4-25 i.e. remove 0 from 04.
If the date is 1 I need to display 1 not 01
Does anyone know this?
Use the PHP date function. In your case I would say date("n-j", $var2);
Too many ways to do this, so let's go with one of them:
ltrim( $var, '0' );
I always store timestamps, that way they can easily be used with date() function to convert them to any format I need.
$now = time();
print date("n-d",$now);
Just use date function: http://php.net/manual/en/function.date.php
echo date('n-j', $var2);
try this :
copy below code and past in test.php and after run you get the out put : 4-25
<?php
$date = "04/25";
$temp = strtotime($date);
echo date('n-j',$temp);
?>

Converting date to this format

I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);

PHP Print Date Variable Format

So this should be a real easy question but I can't seem to find a simple answer anywhere.
I'm patching up some PHP code (I'm not a PHP'er) and I have this variable $orderDate. How do I print this variable so that its just M/d/yy h:mm tt?
Update:
So I looked around and saw what $orderDate is. Here's the code:
global $orderDate;
$orderDate = strftime('%c');
print("Order Date: ".date("M/d/Y h:M", $orderdate)."<br />");
so I get this for output:
Dec/31/1969 06:Dec
and should be getting today's date....
echo date("m/d/Y h:m", $orderDate);
echo date("m/d/Y h:m", strtotime($orderDate)); // or this
Depends on what $orderDate contains.
Look into date() since it has there plenty of examples and is pretty simple to use.
UPDATE:
$orderDate = date("M/d/Y h:M");
print("Order Date: ".orderDate ."<br />");
Also check out to see if this works for you.
date function will do that for you.
If $orderDate is an integer time stamp, you probably want strftime. Specifically, I think the call you want would be:
strftime("%D %l:%M %p", $orderDate)
However, I recommend reviewing the web page to make sure I've interpreted what you want correctly.
See the PHP date() function.
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
string date ( string $format [, int $timestamp = time() ] )

Categories