Strtotime won't convert my date accurately - php

I know this is a common problem, but I can't seem to find the solution anywhere. In my last question [complicated date functions - comparing, subtracting I needed to compare timestamps to get an accurate date for some Cisco logs.
The best I can come up with (since the dates don't actually feature the year) is in the format
Mar 1 2013 00:03:55:
from
Mar 1 00:03:55:
But when I ran some tests, strtotime is converting this date as
Jan 1970
Using
print date("M Y", strtotime($c_log))."\n";
Am I going to have to reformat it into a date it can understand? I don't appear to have the DateTime function. What's the simplest way?

Use the date_parse_from_format() function so you can specify the format
http://php.net/manual/en/function.date-parse-from-format.php

Works for me:
echo date('M Y', strtotime('Mar 1 00:03:55')); // outputs Mar 2013
it should default to the current year when one isn't included.

Related

PHP: Convert "13 September 2013 - 23:55" to SQL Server formatted datetime format

Trying to get my head around this, but can't seem to figure it out. I'm using PHP and attempting to convert the user-submitted date/time selection which outputs:
13 September 2013 - 23:55
I would like to convert that to the standard SQL Server format like:
2013-09-13 23:55:00.000
I've messed with the PHP strtotime() function sending it only the "13 September 2013" part, but it only outputs a long (seemingly) random number.
Is there any easier method for this?
Have a go with:
$date = DateTime::createFromFormat('d F Y - H:i','13 September 2013 - 23:55');
echo $date->format('Y-m-d H:i:s');
This lets you specify a format to read from.
strtotime returns the unix timestamp, you need to turn it to date string.
php > echo date('Y-m-d H:i:s', strtotime('13 September 2013 23:55'));
2013-09-13 23:55:00
You have to make two separate functions,
for converting month to numeric
year to two digit number
and after that you can break the user input to its desired three parts.

php strtotime() messes with date of a different year

I am using php to reformat a date and post it to mysql. Everything works great until I pass dates for next year. For example Mon, 14 Jan, 2013 will be translated into 2012-01-16. The format is correct just not the date, I have even tried changing the format I pass it, still no change. Here is what it gets Mon, 14 Jan, 2013 and here is the php that processes it:
$startdate = $_REQUEST['one'];
$start = date("Y-m-d", strtotime($startdate));
any clues as to why the hiccup happens only when we enter a new year, even past years?
Have a look here for the list of all valid formats for strtotime(). The one you're using is not present.
If you want to use date_create_from_format instead, here's how:
date_create_from_format("D, d M, Y", "Mon, 14 Jan, 2013")

PHP strtotime for June returns July

I'm stumped as to why the following PHP strtotime function returns '07' as the month number, rather than '06' when $monthToGet = 'June':
$monthToGet = $_GET['mon'];
$monthAsNumber = date('m', strtotime($monthToGet));
From searching, it appears it may be due to default date parameters (in this case the day and year) as I haven't specified them. Would that be the cause?
Any suggestions appreciated!
TL;DR
You are right
echo date("m", strtotime("June"));
-> 07
However, this does work:
echo date("m", strtotime("1. June 2012"));
-> 06
The problem explained
Today is 31. July 2012 and since you provide only a month, the current day and current year are used to create a valid date.
See the documentation:
NOTE
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
Alternatives
You could use date_parse_from_format() or strptime() to achieve what you want with a slightly different approach.
(Thanks to johannes_ and johann__ for their input)
Fixed with :
$monthToGet = '1 '. $_GET['mon'];
But I still don't get why, since "m" is a valid date format
Today is 31 Jul. So a strtotime with only "June" is interpreted as 31 June => 1 July.
In fact:
echo date("Y-m-d",strtotime("January")); // 2012-01-31
echo date("Y-m-d",strtotime("February")); // 2012-03-02
of course... only today 31 Jul 2012 :) Tomorrow all will works.
You're lucky because you found this bug just today ;)

Convert time from JSON string with PHP

I have an issue with converting time string I get from JSON to another format. Somehow the date is set to minus 24 hours.
Here's object from JSON
[date] => 2011-07-02T00:00:00+02:00
I'm using strtotime() and date()
date('l, d F Y', strtotime($day->date));
But the output looks like this
FRIDAY, 01 JULY 2011
Obviously the date in JSON is Second of July. Does anyone have any idea why this happens? Am I missing something important? Will really appreciate any help!
I think you should use DateTime. It does not depend on hosts TimeZone. Beside the format is valid ISO8601. So DateTime would have not problem at all.
$dt = new DateTime("2011-07-02T00:00:00+02:00");
echo $dt->format("l, d F Y");
// Echos Saturday, 02 July 2011
http://ideone.com/yPp4d
PHP doesn't understand an infinite array of time/date strings. What is 'obvious' to a human, is not so obvious to a computer. Without a specific parser for that exact date format, how is the computer language to understand what the T in your example is for??
PHP strtotime formats will show you what formats PHP can convert a string from, to a time or date object.
Even as the date/time is parsing correctly, your tzcorrection of +0200 is telling PHP to correct for a timezone difference of GMT + 2 hours, which is likely not your correct timezone offset and thus giving you the error.

How to make date time like this?

Hi I am saving data from rss feed url. From that me got date time like this.
Sun, 2 January 2011 03:04:02 GMT+5:30
How to change this date to this format 2nd January 2011, 03:04 PM using php?
any body knows the solution please help me.
You can se the strtotime function to convert the existing string and the 'r' specifier to the date function as follows (looks like you want it in RFC 2822 format, if not tweak accordingly):
date('r', strtotime("Sun, 2 January 2011 03:04:02 GMT+5:30"));
Incidentally, make sure you're setting your local timezone correctly via date_default_timezone_set, etc.
The following functions are useful for taking a string and getting a timestamp back:
strtotime()
DateTime::createFromFormat()
After you have it as a timestamp, you can reformat it using date(). I'm not 100% sure if strtotime() would accept that format, but it should accept it because the format it isn't ambiguous.
echo date("js F Y, h A", strtotime($oldDate));

Categories