I have a form with two datetime fields. The user inputs the date (yyyy-mm-dd) and time (3 boxes; hour, minute, am/pm).
For some reason, the first one isn't getting saved as a 24 hour time.
The following data is the result of entering:
2011-1-1
4:30 PM
I am using strtotime() to convert a string to a datetime format.
$dateOccured = date('Y-m-d H:i:s', strtotime($dateOccurred));
$dateResolved = date('Y-m-d H:i:s', strtotime($dateResolved));
If I use print_r() to look at the results of this, it is showing correctly.
Date Occurred:
[year] => 2011
[month] => 1
[day] => 1
[hour] => 16
[minute] => 30
[second] => 0
[fraction] => 0
[warning_count] => 0
Date Resolved
[year] => 2011
[month] => 1
[day] => 1
[hour] => 16
[minute] => 30
[second] => 0
[fraction] => 0
[warning_count] => 0
Both show the correct time: 16:30 (or 4:30PM). When I look in my database, this is what is shown.
Date Occurred
2011-01-01 04:30:00
Date Resolved
2011-01-01 16:30:00
I know it's a problem with the dateOccurred variable because if I replace it with dateResolved in my query, it gets inserted correctly. What I can't figure out is where the problem is? What am I missing that is causing this?
Thank you.
If it was the database, wouldn't replacing the dateOccurred variable with dateResolved in the query not matter?
Here is the INSERT query
"INSERT INTO
incidents (
incidentNumber, date, itFunction, issue, severity,
owner, dateOccurred, dateResolved, locationsImpacted, businessImpact,
rcaRequired, rcaReceived, rootCause, notes)
VALUES (
'{$incidentNumber}', '{$date}', '{$itFunction}', '{$issue}', '{$severity}',
'{$owner}', '{$dateOccurred}', '{$dateResolved}', '{$locationsImpacted}', '{$businessImpact}',
'{$rcaRequired}', '{$rcaReceived}', '{$rootCause}', '{$notes}')"
They are both datetime fields.
Found the problem...typos! I've been bashing my head against the wall for an hour trying to figure this out! $dateOccured (single R...rrrrrr)! Put this one in the embarrassing column.
I learned that error reporting can be made more useful by using
error_reporting(E_ALL | E_NOTICE)
Thanks to Chronial for that.
Related
I have some date, like:
20 November 06:10
12 November 08:12
10 October 13:23
There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?
Try this:
$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");
foreach($dates as $d){
$exploded = explode(" ", $d);
$newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);
//print_r($newDate);
$time = strtotime(implode($newDate));
echo $time."<br/>";
}
The output i got is:
1349868180
1352704320
1349868180
The logic is:
You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.
This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.
I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.
You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.
print_r(date_parse_from_format("d F H:i", '20 November 06:10'));
gives you:
Array
(
[year] =>
[month] => 11
[day] => 20
[hour] => 6
[minute] => 10
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
I am currently looking at creating a script for my site that will count down to sunday of that week, every week.
Example:
The user visits the site on a saturday at 11:30am, they will be greeted with:
"The next time this page will be updated is in 0 days, 12 hours and 30 minutes."
Any ideas?
You can use this little trick to get a timestamp for midnight next Sunday:
$sunday = strtotime('next Sunday');
See this answer for how to format it into something useful. Right now I get this:
print_r(dateDifference($sunday, time()));
Array
(
[years] => 0
[months_total] => 0
[months] => 0
[days_total] => 0
[days] => 0
[hours_total] => 4
[hours] => 4
[minutes_total] => 256
[minutes] => 16
[seconds_total] => 15387
[seconds] => 27
)
I am using similar to this solution in one of my pojects. You can use it like this:
ago(strtotime("next sunday")) but you need to change $difference = $now - $time; to $difference = $time - $now;
Here's one solution:
http://jsfiddle.net/foxbunny/xBE7L/
It also automatically updates every second.
Edit: I've included the offset parameter, and you use it to supply the difference between user's and server's time-zone if necessary.
I'm currently writing a script that would extract all the dates from a message and convert them to timestamps. PHP's strtotime (similar to Unix's date -c 'some date') would be perfect for this, as it recognizes all kinds of dates, such as:
5pm today
2010-11-15 16:30
Thursday 8:00
However, I'm having trouble finding those dates in the first place. For example, in the following string,
I'll be there for dinner tomorrow at 9:00pm
I need to isolate "tomorrow at 9:00pm", as that's the part that strtotime recognizes.
Is there a regular expression or something similar that would return me all dates that can be parsed by strtotime?
The only thing I can think of is date_parse. A regular expression that matches any format accepted by strtotime would be huge.
An example of date_parse:
$str = "I'll be there for dinner tomorrow at 9:00pm";
$parsed = date_parse($str);
print_r($parsed);
It would output something like this (I removed the unimportant parts from it to make it the result lighter):
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 21 // 9:00pm
[minute] => 0 // 9:00pm
[second] => 0 // 9:00pm
[fraction] => 0
[warning_count] => 1
[is_localtime] => 1
[zone_type] => 2
[zone] => -540
[is_dst] =>
[tz_abbr] => I
[relative] => Array
(
[year] => 0
[month] => 0
[day] => 1 // tomorrow (would be -1 for yesterday, etc.)
[hour] => 0
[minute] => 0
[second] => 0
)
)
Whether this works for you depends primarily on what your input looks like. If you have more than one instance of a date in your input string, it will not work as expected.
This might not be totally efficient, but should work for any date string that consists of up to 5 words in length. I would write the function, but I think you'll get the idea with the comments below...
$words = explode(' ',$original_string);
// Use the array_chunk() function break up this array into 1-word,
// 2-word, 3-word, and 4-word long substrings of the original string
// Reform them back into strings and pass each one through strtodate()
I have :
$date = $actualite['date'];
$actualite['date'] is a TIMESTAMP
And I was wondering how can I extract from this timestamp the day, then the month, then the year in 3 variables.
Thank you for your help :)
Use date_parse($actualite['date']);, which will return an array containing the day, month, year and other items.
http://www.php.net/manual/en/function.date-parse.php
Example:
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
Output:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
You can extract the values directly within your MySQL query
SELECT DAY( <TIMESTAMP_FIELD> ) AS DAY,
MONTH( <TIMESTAMP_FIELD> ) AS MONTH,
YEAR( <TIMESTAMP_FIELD> ) AS YEAR
FROM <TABLE>
Another way with more options for formatting would be:
$date = date_create($myTimeStamp); // From database "2020-04-09 17:59:20"
$formatedDate = date_format($date, "d/m/y"); // --> 09/04/20
https://www.php.net/manual/en/datetime.format.php
It might be less intuitive than date_parse() but gives you more options as far as I can see.
$now = new DateTime('now');
$tomorrow = new DateTime('tomorrow');
$next_year = new DateTime('+1 year');
echo "<pre>";
print_r($now->diff($tomorrow));
print_r($now->diff($next_year));
echo "</pre>";
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 10
[i] => 17
[s] => 14
[invert] => 0
[days] => 6015
)
DateInterval Object
(
[y] => 1
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 6015
)
any ideas why 'days' shows 6015? why won't it show the total number of days? 1 year difference means nothing to me, since months have varying number of days.
A more appropriate bug report to follow would be #51184 which focuses on the problem of Windows reporting 6015 days (non-Windows appears OK).
No feedback has been given as yet with regards to whether the fix for #49778 (which deals with a different issue) affects this or if the problem persists. If anyone here could take a look and provide some feedback, that would be very kind of you.
Please upgrade to the latest php. This error only occurs on php 5.3 VC6.
$now = new DateTime('now');
should be
$now = new DateTime(''2010-01-01 00:00:00'');
more in the manual
http://nl3.php.net/manual/en/datetime.diff.php
Ok, looks like http://bugs.php.net/bug.php?id=49778 is the issue here.
its a bug
http://bugs.php.net/bug.php?id=49778
Thank you for your bug report.
Days is indeed not set when creating a DateInterval using the
constructor. A complication with this is that it is impossible to
determine the number of days when months or years are specified, since
these vary in length. It is possible to fill in the days field in some
cases and leave it 0 in others. In any case, it should be documented
that the days field is not always available.