String Replace Date & Time - php

a line of my XML looks like this:
<observation_time_rfc822>Thu, 09 Oct 2014 22:59:16 +0200</observation_time_rfc822>
I grab it and give it out:
$ob_time= $xml->observation_time_rfc822;
echo $ob_time;
The output looks like this:
Thu, 09 Oct 2014 22:59:16 +0200
But what I need should look like this (yes, the funny '%3A' replaces ':')
2014-10-09+22%3A59%3A16
I think string replace can do this, please someone can help me to find out!
Thank you!

Edit: Use #Ghost's solution, it correctly handles the timezone offset.
First you need to reformat your date. You do this by parsing it with strtotime and formatting it with the date function. Those "funny %3A replaces" are actually URL-encoded characters:
$date = date('Y-m-d H:i:s', strtotime($ob_time));
$date = urlencode($date); // 2014-10-09+20%3A59%3A16

You could use DateTime class in this case, then use urlencode():
Example:
$ob_time = (string) $xml->observation_time_rfc822;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $ob_time);
$real_date = $date->format('Y-m-d H:i:s');
echo urlencode($real_date); // 2014-10-09+22%3A59%3A16

Related

PHP date_format(): How to format date from string value

I have a bit of PHP code:
$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
Which is used for formatting the date. The expected output would be 2015-12-01 but it returns 2016-12-01. What am i missing?
Use createFromFormat method first, provide the input format:
$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;
The date_create() function accepts only the parameter link, This function is also and alias function of DateTime::__construct()
check the function date_create_from_format() its also a alias function of DateTime::createFromFormat(). Refer link
$exd = date_create_from_format('j M, Y', '01 Dec, 2015');
//$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
It can be a date function call simply. Use stringtotime for exact/precise date/time value
date("Y-m-d",strtotime("01 Dec 2015"))
When you run this code the out put will show
2015-12-01
this is because of the comma in the string which terminates the date string in the compiler. If you specify exactly the timezone (like
$timezone = 'America/New_York) . parameter you can show precise time as well.
i got the solution of your bug
that is date_format(datae_variable,date_format);
<?php
$exd = date_create('01 Dec, 2015');
$exd1 = date_format($exd,"Y-m-d");//here you make mistake
echo $exd1;
?>

To change the date format

Sir,
$datfrs=$_REQUEST["datfrs"];
This is my php code here the $datfrs is the date and its format is (01 / Nov / 2013 ),I need to convert it into (2013-11-01) to fetch the value from data base.I looked through date function but the date format is changing to (1970-01-01) but I need it as(2013-11-01) .Please help me.
Something like this perhaps...
$dt = DateTime::createFromFormat('d/M/Y', $datefrs);
$formatted = $dt->format('Y-m-d');
The first argument to DateTime::createFromFormat should match the incoming format. I can't quite tell if the parentheses and spaces are included. Taken literally, the format would be something like
'(d / M / Y)'
try this
date("Y-m-d", strtotime(str_replace("/", " ",$_REQUEST["datfrs"])));
Try:
date("Y-m-d", strtotime($datfrs))
Use this function to get your required date. This will surely work.
$datfrs=$_REQUEST["datfrs"];
$newdate=MyDateFormat($datfrs);
function MyDateFormat($date) // format is (01 / Nov / 2013 )
{
$exp=explode("/",$date);
$mon=date("m",strtotime($exp[1]))
$ndate=$exp[2]."-".$mon."-".$exp[0];
return $ndate;
}

how to convert timestamp to date in codeigniter

I want to convert 1373892900000 to Monday 2013/07/15 8:55 AM in Codeigniter.
However, I keep receiving a totally different result by converting the timestamp using the function i have written, please note:I need to change the dates according to different timezones, that is why I want to write it this way:
public function time_convert($timestamp){
$this->load->helper('date');
date_default_timezone_set('UTC');
$daylight_saving = TRUE;
$timezone = "UM4"; //toronto or new york timezone
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$final_time = standard_date('DATE_RFC822', $time);
return $final_time;
}
Result from the above function is: Sat, 08 Dec 06 01:40:00 +0000
And if I don't put date_default_timezone_set('UTC'); in the above function, I get this date instead Sat, 08 Dec 06 02:40:00 +0100. My codeigniter seems to default the timezone to Europe/Berlin.
Can anyone please help me correct any of the mistakes I might have made?
Why not just use PHP's date function?
public function time_convert($timestamp){
return date('l Y/m/d H:i', $timestamp);
}
For different timezones use a DateTime object:
public function time_convert($timestamp, $timezone = 'UTC'){
$datetime = new DateTime($timestamp, new DateTimeZone($timezone));
return $datetime->format('l Y/m/d H:i');
}
Think that should work. Note: I tihnk you need at least PHP version 5.20 for the TimeZone class.
<?php
$time_str=1373892900000;
echo gmdate("fill with your format", $time_str);
?>
your format = format your time in php, reading this page for details.
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.gmdate.php
Appears as though an invocation of standard_date with the DATE_ATOM format may sort you:
echo unix_to_human(time(), true, 'us'); # returns 2013-07-12 08:01:02 AM, for example
There are a whole host of other options for the format, enumerated on the linked page.
This how to covert timestamp to date very simple:
echo date('m/d/Y', 1299446702);
to convert timestamp to human readable format try this:
function unix_timestamp_to_human ($timestamp = "", $format = 'D d M Y - H:i:s')
{
if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
}
$unix_time = "1251208071";
echo unix_timestamp_to_human($unix_time); //Return: Tue 25 Aug 2009 - 14:47:51
if you want to convert it to a format like this: 2008-07-17T09:24:17Z than use this method
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
for details about date:
http://php.net/manual/en/function.date.php
Your timestamp is coming from javascript on the client, I would guess, because it appears to be in milliseconds. php timestamps are in seconds. So to get the answer you want, first divide by 1000.
Showing the full year would have made the issue more obvious, as you would have seen the year as 45,506.

Convert int-date to RSS PubDate RFC 822 PHP

I have a question that is making me crazy,
My Task is to parse a date from an API and transform it to RFC 822 format, because the feed that is coming out gets an validation error
the date from the API looks like this :
<review created="2012-10-23 14:51:12.0">
I have one Date in the description made via substr
$xtime = substr($review["created"], 0, 16);
$jahr = substr($xtime,0,4);
$mon = substr($xtime,5,2);
$tag = substr($xtime,8,2);
$datneu = $tag.'.'.$mon.'.'.$jahr;
this date will be rendered like :
23.10.2012
For the pubdate I made
$xtime = substr($review["created"], 0, 16);
$xxl = $pubtime . " GMT";
rendered like :
2012-10-23 14:51:12 GMT
And W3C feed validator says it´s not validate because pubDate is not in RFC 822 form
Sorry
This feed does not validate.
line 10, column 38: pubDate must be an RFC-822 date-time: 2012-10-29 11:51:23 GMT (5 occurrences) [help]
<pubDate>2012-10-29 11:51:23 GMT</pubDate>
and it needs to look like :
<pubDate>Wed, 02 Oct 2002 13:00:00 GMT</pubDate>
i can imagine a hacky solution for expressing sth like "if (month = 01){ actualmonth = Jan}" but i really don´t know how to do same with the days,
Also i´m not too comfortable with PHP but I need to solve this asap.
Hope you can help me, there must be a solution i didnt found at similiar questions
regards John
Have a look at DateTime::createFromFormat() or date_create_from_format.
http://fr2.php.net/manual/en/datetime.createfromformat.php
<?php
$date = date_create_from_format('Y-m-d H:i:s', '2012-10-23 14:51:12.0');
echo date_format($date, 'D, d M Y H:i:s');
?>
Have a look at the possible date formats
http://fr2.php.net/manual/en/function.date.php
EDIT: fixed
Hi in PHP you should look at this: function date
yeaaaaah that worked for me, amazing!
for others the exact solution for my Case was
$before = "2012-10-23 14:51:12.0";
$timetwo = substr($before, 0, 16);
$timethree = date_create_from_format('Y-m-d H:i:s', $timetwo);
$timefinal = date_format(timethree, 'D, d M Y H:i:s');
$after = $timefinal . ' GMT' ;
$after = "Mon, 23 Oct 2012 14:51:12 GMT";
thanks a lot for the quick answers you are awesome!
This worked for me....
date("r", strtotime($yourdate))
$yourdate was 2013-10-27 and I got Sun, 27 Oct 2013 00:00:00 +0000

PHP Timestamp into DateTime

Do you know how I can convert this to a strtotime, or a similar type of value to pass into the DateTime object?
The date I have:
Mon, 12 Dec 2011 21:17:52 +0000
What I've tried:
$time = substr($item->pubDate, -14);
$date = substr($item->pubDate, 0, strlen($time));
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
The above is not correct. If I loop through a lot of different dates its all the same date.
You don't need to turn the string into a timestamp in order to create the DateTime object (in fact, its constructor doesn't even allow you to do this, as you can tell). You can simply feed your date string into the DateTime constructor as-is:
// Assuming $item->pubDate is "Mon, 12 Dec 2011 21:17:52 +0000"
$dt = new DateTime($item->pubDate);
That being said, if you do have a timestamp that you wish to use instead of a string, you can do so using DateTime::setTimestamp():
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);
Edit (2014-05-07):
I actually wasn't aware of this at the time, but the DateTime constructor does support creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an # character:
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime('#' . $timestamp);
While #drrcknlsn is correct to assert there are multiple ways to convert a time string to a datatime, it's important to realize that these different ways don't deal with timezones in the same way.
Option 1 : DateTime('#' . $timestamp)
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
The strtotime bit eliminates the time zone information, and the date_create function assumes GMT.
As such, the output will be the following, no matter which server I run it on :
2011-12-12T13:17:52+00:00
Option 2 : date_create()->setTimestamp($timestamp)
Consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
You might expect this to produce the same output. However, if I execute this code from a Belgian server, I get the following output :
2011-12-12T14:17:52+01:00
Unlike the date_create function, the setTimestamp method assumes the time zone of the server (CET in my case) rather than GMT.
Explicitly setting your time zone
If you want to make sure your output matches the time zone of your input, it's best to set it explicitly.
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Now, also consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Because we explicitly set the time zone of the output to match that of the input, both will create the same (correct) output :
2011-12-12T21:17:52+08:00
Probably the simplest solution is just:
DateTime::createFromFormat('U', $timeStamp);
Where 'U' means Unix epoch. See docs: http://php.net/manual/en/datetime.createfromformat.php
This is my solution:
function changeDateTimezone($date, $from='UTC', $to='Asia/Tehran', $targetFormat="Y-m-d H:i:s") {
$date = new DateTime($date, new DateTimeZone($from));
$date->setTimeZone(new DateTimeZone($to));
return $date->format($targetFormat);
}

Categories