php datetime custom formatting - php

I'm trying to use
format("m/d/y \a\\t h:ia T");
to get
05/14/15 at 04:22am MDT
which used to work but now all of a sudden I'm getting
Exception: DateTime::__construct() [datetime.--construct]: Failed to parse time string (05/14/15 at 04:22am MDT) at position 9 (a): The timezone could not be found in the database in some/folders/and.files on line x
So my work around for now is taking the "at" out
format('m/d/y h:ia T');
I've found I can leave the \a in with no problem but the \a\\t causes the error. Any on how to get past this?
**edit
The timestamp being passed in is generated by mysql
2015-05-14 10:22:41
**edit
This accepted answer didn't actually solve the problem for me. I am certain the issue is unique to my server / application so once I figure it out on my own I'll post back here as to avoid any further downvotes.

I'm working with a LOT of data and a LOT of files (classes) to get this thing working so I couldn't paste all my code in here or else no one would look at this question. So here's the skinny on what was actually happening:
Inside of a loop my time value was getting replaced with the formatted value and then getting caught up in the same loop, hence why I was getting that error and everything thought I was getting time with an incorrect value because ultimately I was (the first time it was okay but not on the re-run).
resolution
When in doubt, print out the time value going INTO your DateTime->format and you may be surprised that it's not what you think it is.

Related

Time Format for Filemaker PHP

I'm having an issue with the FileMaker PHP API. I don't know how to format a time correctly for input into a Time field.
I tried entering a string with the format that the documentation guide says (H:M where H can be 24 hours) and it still doesn't take. I don't know if I'm supposed to change the data type with another function like date() or what.
Edit: A little snippet of code
//Date
if($arrDate!=NULL){
$booking->setField('arrival_date', '07/17/2015');
}
//Time
if($arrTime!=NULL){
$booking->setField('arrival_time', '11:00:00');
}
$savedBooking = $booking->commit();
I replaced the variables for the value fields with what they should be just in case it was something going wrong with them, but it still doesn't work. The date field setting works fine if I comment out the Time section however.
Here's the format that I use:
$booking->setField('arrival_date', date ( "n/j/Y H:i:s A" ) );
If you're interested in adjusting the time, here's a post to checkout: http://timdietrich.me/blog/php-timezone-arithmetic/
Good luck!
I suspect you may have some validation set up on the "arrival_time" time field. What is the error that is returned by the API? It should return an error code that will tell you more.
Also worth mentioning that FileMaker will inherit the date and time settings from your system settings. For example, date formats are different in Europe than they are in the states. But that time value you give looks like it would work, and you only get an error when setting that field, so I would look to make sure the field is defined correctly.
You can also test by putting that value in via FM Pro and see what happens.
I'm an idiot, after hours struggling with this I realized I had put the wrong field name! Thanks for your help guys. A string of H:M worked perfectly.

Change RSS feed, but only new items

I'm fairly new to PHP, and I'm trying to write a script that solves the following
I have an RSS feed that gets saved to my server every 10 minutes (copied from elsewhere).
There is a problem with the timestamps (pubDate tag) on the RSS feed, they always have the correct date but 00:00:00 GMT as the timestamp (I have no control over this).
Therefor, when I use an autotweeting program to tweet updates from the feed (it checks it every hour or so), it won't - It only tweets the first update of each day as a result.
Therefor, what I'm trying to do to fix it to some degree is to check if the feed has changed, and if it has, change the saved pubDate to the current server time on only the new items.
I'm also kind of confused as to how I can have it check for changes - If I have a corrected version (with fairly accurate timestamps) saved to my server, it will ALWAYS think there are changes, because the timestamps will always be 00:00:00. I'm thinking, check both feeds for items including the full strings such as <guid isPermaLink="true">http://services.runescape.com/m=adventurers-log/a=161/display_player_profile.ws?searchName=A13d&id=-463827091</guid> - Since the id= at the end stays constant, it would only change the <pubDate> of items found to be new.
http://services.runescape.com/m=adventurers-log/a=161/rssfeed?searchName=A13d Here is a feed as an example. If anyone could get me started or point me to some kind of tutorial that might help, I'd really appreciate it. This is over my head, but something I need to learn how to do.
Maybe there is something wrong with your code parsing the timestamp, date format perhaps?
I believe the method of doing full string comparisons(<title> & <description>) between items with the same <guid> is your best bet. Here is some reading about RSS duplicate detection if you are interested.

PHP Looping through dates

I have the following while loop which compares 2 dates to see if 1 is less than another, and if not then it adds a week to the first date.
It works fine, however where there is a greater difference between the dates (i.e. greater than 12 months) I get a timeout error as it has passed the max execution time.
Can anyone help me make this more efficient?
while($startDate->timestamp < $current_week->timestamp){
$data['weeks'][$w] = array(
'monday' => $startDate->startofWeek()->format('d/m/Y'),
'sunday' => $startDate->endofWeek()->format('d/m/Y')
);
$w++;
$startDate = $startDate->addDays(7); // Move it on to the following week
}
I should add that I'm working with the Carbon API extension for DateTime
Thanks
----Edit----
Something strange is happening that I don't understand. If I pass a date hard coded to $startDate everything after 2013-01-06 works fine (and fast) but as soon as goes after this date it times out. Any ideas anyone?
It looks like you're doing a little more to $startDate than you expect - you're modifying the object with ->startOfWeek() and ->endOfWeek() rather than just calculating the string output; my reading of the source to Carbon is that every method you call modifies the object, so the assignment in the last line of your while loop is unnecessary (it could just as easily be written as $startDate->addDays(7);. As a result of that your code is a little bit less efficient than it could be, and might even loop one or two times more than you want.
However, I would suspect this block of code isn't your real problem, and that it's further down your codebase - if your problems grow with the size of $data['weeks'] then it's probably something in the way in which you're processing it for your HTML.

PHP strtotime not recognizing "2012-W11" on new server

Sorry if this seems like a stupid question. I'm probably missing something obvious but I've been struggling with this for ages and am getting nowhere. I've tried searching but haven't been able to find anything specific enough so I'm asking here in the hope that someone smarter than me can help me out.
I'm using the following line of PHP code to get the timestamp of the beggining of the current week.
strtotime(date('o-\\WW', time()));
This works fine on my local server and it worked fine on my live shared hosting, but now that I have moved my site to a virtual dedicated server it returns a blank.
The date() part is working fine on all servers ie.
date('o-\\WW', time()) today returns 2012-W11 on all servers
Therefore the problem is with the way strtotime turns 2012-W11 into a timestamp ie a problem with running strtotime('2012-W11')
I'm guessing there must be a difference in the way the new virtual dedicated server is set up that is causing it not to be able to do this in the expected way but I can't for the life of me work out what it is.
Thanks in advance for your help
try changing the format to strtotime('2012 +11 weeks')
you can use below code get first day of the current week
$weekFirstDay = strtotime('Monday this week');
it's from PHP Manaul

Reading date from Excel using PHP giving one day more

I am trying to read dd/mm/yyyy date from Excel. It is behaving very inconsistently. At times it adds one day more to the actual date in the cell and at times give the correct date.
The code:
$pdiDate_key = array_search('PDI Date',$excel->sheets[0]['cells'][1]);
Based on your code example, I'm going to guess that you're using PHP-ExcelReader to retrieve the dates from Excel. If I'm right, then, well, there is a known error in Excel that PHP-ExcelReader doesn't handle. The "bug" report is here:
SourceForge.net: PHP-ExcelReader: Detail: 2388545 - Wrong Date
The comments at the bottom of that page include code for a function to fix the error.

Categories