iam having the date format stored in database as "20100723"(YYYYMMDD) and how do i convert it into "23-JUL-2010"
$original = "20100723";
$converted = date("d-M-Y", strtotime($original));
See manual: date(), strtotime()
if php >= 5.3.0
<?php
$date = DateTime::createFromFormat('YYYYMMDD', $dbStoredTime);
echo $date->format('d-M-Y');
strtoupper(date_create("20100723")->format("d-M-Y"))
See the DateTime class, in particular the DateTime::format method. date_create is an alias to the constructor of DateTime.
Use PHP's strtotime() function, it recognizes the date format in the string and converts it into a Unix timestamp.
$time = strtotime("20100723"); // 1279836000
Then just use date() to convert it back into another string format
echo date("d-M-Y", $time); // 23-Jul-2010
Note that you would have to use strtoupper() to make "Jul" upper-case
Related
I need to convert DD/MM/YYYY h:m eg 21/12/2015 17:14 this date to timestamp format using php and store it in mysql.
I have tried strtotime() and some other functions but it's not working. Can any one help me on this. I'm using datepicker to generate the value for date time
Use following code to convert "DD/MM/YYYY h:m" into timestamp
$dateString = "21/12/2015 17:14";
$timestamp = strtotime(str_replace("/", "-", $dateString));
you can use DateTime class:
$dateStr = '21/12/2015 17:14';
echo $timestamp = \DateTime::createFromFormat('d/m/Y H:i', $dateStr)->getTimestamp();
I have made many searches for a simple function to convert custom DateTime to Timestamp, But I didn't get the exact solution:
The DateTime str is:
$dt = "14-05-2015 18:21";
(Day-Month-Year Hour:Minute) (not the standard DateTime format)
Is there a simple function to convert this string to timestamp?
Thanks.
can't be simpler than:
$dt = "14-05-2015 18:21";
echo strtotime ($dt); //1431627660
If format will always be the same, you can do it like this.
$date = DateTime::createFromFormat('d-m-Y H:i', $dt);
$timestamp = $date->format('U');
I am parsing a feed which returns the date in form:
20150129180000
which should be [yyyy][mm][dd][hours][minutes][seconds]
I tried formatting this with the php date() function:
date('Y-m-d H:i:s', $event['start-dt'])
and got this:
640502-03-05 12:33:20
which is obviously not right. Is there a smooth way of converting this, or do I simply need to splice the string manually?
Doc
$date = DateTime::createFromFormat('YmdHis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
You can use preg_replace to format the string
$date_str = preg_replace("/^(\d){4}(\d){2}(\d){2}(\d){2}(\d){2}(\d){2}/","$1-$2-$3 $4:$5:$6",$event["start-dt"]);
date('Y-m-d H:i:s', strtotime($date_str));
Or use php's DateTime object
$date = new DateTime($date_str);
Or better yet, use the DataTime createFromFormat method
$format = "YmdGis";
$dateObj = DateTime::createFromFormat ( $format, $event['start-dt'] );
Then you can use getTimestamp to retrieve the timestamp for calculations, if you need it.
The format is somewhat confusing compared to date until you are used to it, but this corresponds to your date string:
$date = DateTime::createFromFormat('YmdGis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
When I had UNIX timestamps, I'd write:
strftime("%A", $date)
But now I have datestamps, like "2011-08-02"
How can I make it output the weekday name, e.g "Sunday"?
You can convert the date stamp to a timestamp using the function strtotime.
Once you have the timestamp, you can just use the function date to show the date in the desired format.
First use the strtotime function to convert the '2011-08-02' to a UNIX timestamp, and then proceed as you usually would
For example, the following are equivalent:
$date = 1312243200; // A unix timestamp
$date = strtotime('2011-08-02'); // The date that it represents
You can then do whatever you would usually do with the result
The strtotime() function is fairly forgiving in what date formats it accepts and even accepts values such as '8pm tomorrow' or 'last Monday' - See http://www.php.net/strtotime
Use date('l'); Add more properties like so: date('l d-m-Y');.
More info here
(2nd August 2011 was a Tuesday, not a Sunday.)
<?php
echo strftime("%A", strtotime("2011-08-02"));
// Output: "Tuesday"
?>
Live demo
strtotime documentation
date('l',strtotime('2011-08-02'));
I need to format a timestamp in ISO 8601 format (e.g. 2001-10-26T21:32:52). When I use the date() function in PHP, it replaces T with the Timezone (as it is supposed to do).
The command I'm using is:
$time = date("y-m-dTH:i:s", time());
This produces: 10-02-13EST10:21:03
How do I get it to insert an actual T and not replace with EST?
Your format shoule be : "c"
$time = date("c", time());
From PHP manual:
Format Descriptions Example
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
If you need to insert a character which should not be interpreted, precede it with a backslash:
$time = date("y-m-d\TH:i:s", time());
You could format the date and time parts seperately, then concatenate the two parts with "T":
<?php
$time = time();
$time = date( "y-m-d",$time )."T".date( "H:i:s", $time );
?>
DATE_ATOM is provided for this format:
$theStart_date = date(DATE_ATOM, strtotime($start_date));
Output:
2013-04-10T09:10:30-04:00