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")
Related
I am converting two strings from one date format to another but I am getting some unusual responses
The two dates i am converting are:
1: 2002-09-23
2: 2010-03-25
The PHP code is as follows for each of the dates:
1:date('d F Y', strtotime((string)$report_display->arr_output_base['Date1']['value']));
2:date("d F Y", strtotime((string)$report_display->arr_input_base['Date2']['value']));
The responses I'm getting are as follows:
1:Sometimes 31 December 1986 which is wrong but then other times I'll get 23 September 2002 which is right
2: is always 25 March 2010
When you know the format, why take risks sending it to strtotime and not use a proper method that uses the exact format?
$date = DateTime::createFromFormat('Y-m-d', '2002-09-23');
echo $date->format('d F Y');
That way there is no guesswork involved as to whether a month is a month or it is a day. That will always return the same date no matter what.
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.
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.
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 ;)
I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.