When using the following
echo date('D',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D',strtotime("2013-06-16T18:00:00-07:00"));
First it returns Sun and the Second returns Mon. I'm not really sure why or how to correct! The Date:"2013-06-16T06:00:00-07:00" is data I'm retrieving from a XML file. The timestamp has the correction for UTC at the end not sure if this is generating the error.
Thanks for any help.
To get expected results you should consider using DateTime():
<?php
echo date('D',strtotime("2013-06-16T06:00:00-07:00")) . "\n";
echo date('D',strtotime("2013-06-16T18:00:00-07:00")) . "\n";;
$dt1 = new DateTime("2013-06-16T06:00:00-07:00");
$dt2 = new DateTime("2013-06-16T18:00:00-07:00");
echo $dt1->format('D') . "\n";
echo $dt2->format('D') . "\n";
Output
Sun
Mon
Sun
Sun
Fiddle
This is because The Date represents the time is in time zone specified in date.timezone settings. So the timezone -07:00 is parsed and converted back to date.timezone timezone.
To understand the idea just add e in the date string
echo date('D e',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D e',strtotime("2013-06-16T18:00:00-07:00"));
See example.
Its better you use DateTime(). It does not have such limitation.
Related
I receive dates in the following format 2015-01-09T20:46:00+0100 and need to convert it in timestamp.
Unfortunately, strtotime function is ignoring the timezone component :
print strtotime('2015-01-09T20:46:00+0100') . "\n";
print strtotime('2015-01-09T20:46:00');
//result is the same with or without timezone:
//1420832760
//1420832760
What is the right way to solve this issue ?
DateTime handles this correctly:
$date = new DateTime('2015-01-09T20:46:00+0100');
echo $date->getTimestamp();
echo "\n";
$date = new DateTime('2015-01-09T20:46:00');
echo $date->getTimestamp();
1420832760
1420836360
Demo
I fugured out !
uses the default time zone unless a time zone is specified in that
parameter
http://php.net/manual/en/function.strtotime.php
That's why the result is the same setting or not the timezone :
date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
print "\n\n";
date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
output:
1420829160
1420832760
1420832760
1420832760
1420836360
Demo: https://eval.in/241781
Thanks for the help!
I try to calculate the easter date in php.
echo(date("2012: t.n.Y", easter_date(2012)).'<br>'); // 2012: 30.4.2012
This date is correct for the eastern orthodox churches. But I want the normal one!
My next try with the easter_days function:
function easter($year) {
$date = new DateTime($year.'-03-21');
$date->add(new DateInterval('P'.easter_days($year).'D'));
echo $year.": ".$date->format('t.m.Y') . "<br>\n";
}
easter(2012); // 2012: 30.4.2012
Tested oh PHP 5.2.6 and 5.3.6. I also tried to change the timezone with no success.
Your date format is wrong. t is the number of days in the given month (april = 30). Use d for day of the month:
echo(date("d.m.Y", easter_date(2012)).'<br>');
// will output: 08.04.2012
btw: orthodox easter date is April 15th this year.
If you want to use the DateTime class, the following will give you a DateTime object set to Easter. Use easter_date() instead of fiddling around with easter_days():
function easter($year, $format = 'd.m.Y') {
$easter = new DateTime('#' . easter_date($year));
// if your timezone is already correct, the following line can be removed
$easter->setTimezone(new DateTimeZone('Europe/Berlin'));
return $easter->format($format);
}
echo easter(2012); // 08.04.2012
echo easter(2012, 'd.m.Y H:i'); // 08.04.2012 00:00
Timezone
Setting the timezone is only necessary when the default timezone is wrong. Must be set afterwards as it is ignored in the constructor when a unix timestamp is provided.
If left out, the DateTime constructor may produce a wrong date (e.g. 07.04.2012 22:00 for 2012 instead of 08.04.2012 00:00)
I want to generate a timestamp in PHP and then store that value in SQLite.
Here is the code:
$created_date = date("YYYY-MM-DD
HH:MM:SS",time());
Here is what gets stored in the DB (which looks wrong):
11/21/0020 12:39:49 PM
How should I change my code to store the current date/time properly in SQLite
Why not just update your SQLite query to use date('now')? reference
But, within PHP you should be able to use
$created_date = date('Y-m-d H:i:s');
I just implemented this for my own project..
My solution was to use the UTC time format officially specified for the web: RFC3339.
You can use any of the following PHP time format constants:
DATE_RFC3339
DATE_W3C
DATE_ATOM
Example:
$sqlite_timestamp = date(DATE_RFC3339);
echo "My SQLite timestamp = ".$sqlite_timestamp;
The best part is the alphanumeric order corresponds to the date order, so you can use ORDER BY SQL statements on the date fields!
How do you check what the stored value is? Also, what is the datatype of the column in the database? Are you aware that SQLite is typeless, so all values are being stored as strings or integers?
$created_date = date('Y-m-d H:i:s');
echo $created_date ."<br />\n";
$created_date = date('l, F jS, Y - g:ia');
echo $created_date ."<br />\n";
$created_date = date('n/j/y H:i:s');
echo $created_date ."<br />\n";
$created_date = date('r'); // RFC 2822 formatted date
echo $created_date ."<br />\n";
$created_date = date('c'); // ISO-8601 formatted date
echo $created_date ."<br />\n";
Output could be:
2011-04-01 19:33:40
Friday, April 1st, 2011 - 7:33pm
4/1/11 19:33:40
Fri, 01 Apr 2011 19:33:40 +0200
2011-04-01T19:33:40+02:00
Be aware that date() function depends of default timezone set in PHP config. You can always test it using echo date_default_timezone_get(); or set it by date_default_timezone_set('TIMEZONE_IDENTIFIER'); where TIMEZONE_IDENTIFIER is one from the list of supported timezones.
In PHP, how do you convert a mysql timestamp like this "1125443836"
to an xml/date-time like this:
<wp:post_date>2011-01-25 02:10:32</wp:post_date>
UPDATE:
The columns in the mySQL are stored as int(10).
Based on samples below, this is what I tried with two sample values in my database.
Something is wrong, maybe hash or Unix dates stored in mySQL table?
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
echo "<BR>";
Results:
DateTest=1969-12-31 18:00:00
DateTest=1969-12-31 18:00:00
Second Update: worked without calling strtotime
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
Results:
DateTest=2005-08-30 18:21:47
DateTest=2005-08-30 18:17:16
'Y-m-d G:i:s' is not it. At least not in 2015 (I realize this is old).
In php 5.2.0 the DateTime class has a constant for this:
echo 'XML time: ' . date( DateTime::RFC3339, time() ) . '<br>';
DateTime::RFC3339 is set to "Y-m-d\TH:i:sP".
date("Y-d-m G-i-s",$time);
That should do it.
http://php.net/manual/en/function.date.php
EDIT: this won't work for mySQL timestamps, they must be converted to unix timestamps via strtotime() info: PHP: strtotime - Manual.
<?php echo '<wp:post_date>' . date('Y-m-d G:i:s',strtotime($yourMysqlDateStampVar)) . '</wp:post_date>';
How do I get the system date and time in this format:
$systime="12/january/2010 10.30 AM "
To get exactly what you've asked for, you'll need to use strtolower() and date:
$systime = strtolower(date("d/F/Y G.i")) . " " . date("A") . " ";
You need strtolower because there's no built-in way to get lowercase month values, so you need to get that part as January and then transform it to lowercase. You can't lowercase the whole thing because you seem to want AM or PM rather than am or pm.
Using date and strftime() we can get the system date.
Example:
echo date("d/F/Y g:i A");//It prints 05/March/2010 12:18 PM
echo strftime("%d/%B/%Y %I:%M %p"); //It prints 05/March/2010 12:20 PM
Try:
$systime = date('d/F/o g i A');
Sample output:
05/March/2010 7 27 AM
date() and time()