I would like to remove the last certain characters of following array api variable 'min_date_created' like
$filter = array('min_date_created' => "$start_date");//query filter
Here the values of
'min_date_created'=Mon, 24 Sep 2012 00:53:26 +0000
So i want to remove the last 15 characters, so i expect the following format
'min_date_created'=Mon, 24 Sep 2012
So please any one help me how can i change this array variable 'min_date_created' in required format.
You can format using the date & strtotime function like below
http://in3.php.net/strtotime
<?php
//format the date
$min_date_created = date('D, d M Y', strtotime($start_date));
$filter = array('min_date_created'=>$min_date_created);
Expanding Sundar's Answer , You can achieve simpler using an array_walk to do modify all the array elements in a single go.
<?php
$startdate="Mon, 24 Sep 2012 00:53:26 +0000"; // Usually you will be getting from a POST variable.
$filter = array('min_date_created' => $startdate);
array_walk($filter,'formatDT');
function formatDT(&$v,$k)
{
$v=date('D, d M Y', strtotime($v));
}
print_r($filter);
OUTPUT :
Array
(
[min_date_created] => Mon, 24 Sep 2012
)
You could use substr to remove the last 15 characters from $start_date before using it in the array.
$filter = array('min_date_created' => substr($start_date, 0, -15));
Output
array (
'min_date_created' => 'Mon, 24 Sep 2012',
)
Related
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
The Unix Time Stamp generated by my MKTIME is not generating correctly
(European format)
Date Submitted: 15/02/2014 19:00
Unix Code: 1392508800
This returned date is: 16/02/2014 00:00:00
Code it should of returned is: 1392490800
Code Used
After form is submitted values are captured and assigned...
if ($_POST['Callbacl']){
$CID=$_POST['ID'];
$Notes = $_POST['callbacknotes'];
$Time_H = $_POST['Time_Hour'];
$Time_M = $_POST['Time_Minute'];
$Date_Day = $_POST['Date_Day'];
$Date_Month = $_POST['Date_Month'];
$Date_Year = $_POST['Date_Year'];
$Appt = mktime($Time_H, $Time_M,00, $Date_Month, $Date_Day, $Date_Year);
echo $Appt . "<br>";
die;
}
Post values
Array (
[ID] => 1
[Time_Hour] => 19
[Time_Minute] => 00
[Date_Month] => 02
[Date_Day] => 15
[Date_Year] => 2014
[callbacknotes] =>
[Callback] => Call Back
)
I suppose to your time zone in php.ini is not your preferred one.
Try to set a proper time zone in php.ini or to add the following code at the beginning of your block.
date_default_timezone_set('your time zone');
There is the list of supported time zones in PHP:
http://www.php.net/manual/en/timezones.php
Good !
I am having some difficulties with extracting data from a date. The thing is that I get a number from an undocumented API.
"created": 734394
"last_chapter_date": 734883
I tried dividing it by 365,242 days (exact amount of days a year)
2010,705231052289
So apparently these are the number of days passed since 0.0.0000
I am currently trying something like that:
http://jsfiddle.net/LRUy5/4/
function zero21970(nDays) {
// 0 70 2013
// |-----|-----|
// 0 to date
var dateMils = nDays*24*60*60*100;
// 0 to 1970
zeroTo1970 = (1970*365.242)*24*60*60*100;
//subtract time from 0-1970 from the time 0-date
//to cut out the part from 1970-today
return new Date(dateMils-zeroTo1970);
}
//http://www.mangaeden.com/api/manga/4e70e9f6c092255ef7004344/
zero21970(734394) //-> Jan 26 1974
I need to save it in a database and work with it via php or javascript..
Does anyone recognize this kind of format or do you know a convenient way of formatting it?
Edit: I should add that the last chapter came out around 15.01.2013.. just to have something to grab.
Updated version:
I guess if the last chapter was from 2013, then the value is a number of days from 01.01.0001. So we can update the initial date as well as change setHours to setDate method for more accuracy:
var date = new Date("0001");
date.setDate(734883);
date.toGMTString(); // "Tue, 15 Jan 2013 00:00:00 GMT"
DEMO: http://jsfiddle.net/LRUy5/6/
Old version:
I found one solution that successfully works at my computer:
var date = new Date("0000");
date.setHours(734394 * 24);
date.toGMTString(); // "Mon, 13 Sep 2010 21:00:00 GMT"
DEMO: http://jsfiddle.net/LRUy5/5/
If you're using PHP, then you should replace
return new Date(dateMils-zeroTo1970);
with
return date('Y-m-d', (dateMils-zeroTo1970));
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
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);
}