php date() function print formatted date - php

I would like to print a date format like: Sun, 08 Mar 2015 14:54:54
I use the function date("r"); but the output is: Sun, 08 Mar 2015 14:54:54 +0100
how to remove part +0100?

Use the right parameters:
echo date('D, d M Y H:i:s');

Use DateTime class and it's that easy:
$date = new DateTime();
echo $date->format("D, d M Y H:i:s");
Output:
Sun, 08 Mar 2015 15:00:57

Use following.
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo date('D, d M Y G:i:s');
// Prints something like: Sun, 08 Mar 2015 14:30:10
Read more here

Related

how to get this type of date in php like this Date {Fri Jun 24 2016 10:30:06 GMT+0530 (IST)} jquery standerd format

How to get this type of date in PHP like this
Date {Fri Jun 24 2016 10:30:06 GMT+0530 (IST)}
jQuery standerd format.
Try:
Also refer http://php.net/manual/en/function.date.php for different date formats
echo date("D M d Y h:i:s eO T");
Output:
Fri Jun 24 2016 10:52:42 Asia/Calcutta+0530 IST
As you were looking for ยป RFC 2822 formatted date you can simply use
echo date("r"); // Fri, 24 Jun 2016 10:59:36 +0530
Or if you want to be somewhat like as question you can use
echo date("D M d Y H:i:s eO T"); // Fri Jun 24 2016 10:59:37 Asia/Kolkata+0530 IST
Docs
In PHP you can get this format as
<?php
$tzone = new DateTimeZone('Asia/Calcutta');
$dtime = new DateTime("now", $tzone);
echo $dtime->format("D M d Y H:i:s \G\M\TO (T)");
?>
In that case you will get the output:
Fri Jun 24 2016 11:25:40 GMT+0530 (IST)
That is your output as you wanted.
finally got it .... thanks for help..
$tzone = new DateTimeZone('Asia/Calcutta');
$dtime = new DateTime("now", $tzone);
$daet = $dtime->format("D M d Y H:i:s \G\M\TO (T)");
$timestart = explode (' ', microtime());
$timeInSecond = substr($timestart[0],1,4)."Z";
echo preg_replace('/\+.*/',$timeInSecond,date('c',"1466774626"));
//2016-06-24T15:23:46.541Z

how can I format my date in php to have the query new Date() format?

I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

String to DateTime Object

I'm new at String functions, so I need a complex substr and trim functions for this string:
Wed, 28 Dec 2011 13:04:30 GMT
String comes to me always with this format. I want to convert it to DateTime object. Anybody can help me?
$dateString = 'Wed, 28 Dec 2011 13:04:30 GMT';
$dateTime = datetime::createfromformat('D, d M Y H:i:s e',$dateString);
echo $dateTime->format('d-M-Y H:i:s e');
<?php
$date = new DateTime('Wed, 28 Dec 2011 13:04:30 GMT');
echo $date->format('r');
... prints:
Wed, 28 Dec 2011 13:04:30 +0000
If you are wanting to take a date string and write that to the database as a date object using Doctrine:
Note: This is a form post example for Symfony 3 and 4.
$mynewdateobject = new \DateTime($request->request->get('mydatestring'));
Then you can write the object to the database or use it elsewhere in your code.

Convert date in php

How to convert "Thu Oct 28 16:33:29 +0000 2010 " to like "Oct 28,2010 at 10:33PM"
Use the strtotime function combined with the date function
echo date("jS F, Y", strtotime("11/25/10"));
// outputs 25th November, 2010
$date = DateTime::createFromFormat('Thu Oct 28 16:33:29 +0000 2010', 'D M j h:m:s O Y');
echo $date->format('M d,Y, h:mA');
Relevant docs here

Categories