I have an input box that grabs local time
date_default_timezone_set('America/Los_Angeles');
echo "<input type='datetime-local' name='fromDate' class='dates'>";
When I enter 12-31-2014 10:00:00 PM in the input
echo $_POST['fromDate'];
Response: 2014-12-31T22:00:00
$test = new DateTime($_POST['fromDate']);
echo $test;
I get 2014-12-31T22:00:00 America/Los_Angeles
Then when I convert
$from_dateGMT = new DateTime($_POST['fromDate'], new DateTimeZone('Europe/Paris'));
$from_date = $from_dateGMT->format('Y-m-d\TH:i:s');
echo $from_date;
I get 2014-12-31T22:12:00 UTC, which is the same time listed above and should be adding 8 hours.
What am I doing wrong?
I don't deal with dates/times in PHP ever, so this is a learning experience for me.
Perhaps this will work
$test = new DateTime($_POST['fromDate'], new DateTimeZone('America/Los_Angeles'));
$test->setTimezone(new DateTimeZone('Europe/Paris'));
echo $test->format('Y-m-d\TH:i:s');
At least that is how it is done in php manual here: http://us2.php.net/manual/en/datetime.settimezone.php
Related
I am trying to get time in 2015-03-19T10:57:40.174Z format but its not working for me
I have tried these-
1.
echo date('Y-M-DTHHM:SS.MMMZ');
2.
date_default_timezone_set('utc');
$datetime = new DateTime();
$str = $datetime->format(DateTime::ISO8601);
echo $str;
output-2015-03-19T17:27:46+0530
3.
date_default_timezone_set();
print date('c');
output-2015-03-19T17:33:52+05:30
but i need exact format(2015-03-19T10:57:40.174Z). how can i do that ?
please help.
I have a database table where I store a date NOW(), and the time is set two hours behind me. My timezone is UTC+2, and my phpMyAdmin database is using 'Africa/Accra' or 'Artic/Reykjavik' - GMT.
It’s fine if my database is using this as default, but when I try to convert the time to the users timezone, nothing happens.
This is what I have tried:
date_default_timezone_set("Africa/Accra"); // Also tried with GMT
$timezone = 'Europe/Berlin';
$feed_time = '2014-05-20 19:27:52';
$schedule_date = new DateTime($feed_time, new DateTimeZone($timezone) );
$newdate = date('Y-m-d H:i:s',strtotime("$feed_time UTC"));
echo $newdate;
This echo out: 2014-05-20 19:27:52, so no difference.
How can I convert the time so it would be the same as the device default timezone?
You're not using $schedule_date. You create it with your new values but never actually use it. You still use the old data.
$schedule_date = new DateTime($feed_time, new DateTimeZone($timezone) );
echo $schedule_date->format('Y-m-d H:i:s');
Also, you need to set your date/time first and then change the timezone. This is because your date/time is in UTC so you need to start there. Then you can change the timezone accordingly.
$original_date = new DateTime('2014-05-20 19:27:52', new DateTimeZone('UTC') );
$original_date->setTimeZone(new DateTimeZone('Africa/Accra'));
echo $original_date->format('Y-m-d H:i:s');
$schedule_date = new DateTime('2014-05-20 19:27:52', new DateTimeZone('UTC') );
$schedule_date->setTimeZone(new DateTimeZone('Europe/Berlin'));
echo $schedule_date->format('Y-m-d H:i:s');
Output:
2014-05-20 19:27:52
2014-05-20 21:27:52
Demo
Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue
What is easiest way to display the current time in PST (West Coast) time using PHP?
Well, the easiest might be:
date_default_timezone_set('America/Los_Angeles');
echo date('Y-m-d');
Take a look at supported timezones to find one suitable for your needs.
Let's try a solution that uses PHP's modern date handling. This example requires PHP 5.2 or better.
// Right now it's about four minutes before 1 PM, PST.
$pst = new DateTimeZone('America/Los_Angeles');
$three_hours_ago = new DateTime('-3 hours', $pst); // first argument uses strtotime parsing
echo $three_hours_ago->format('Y-m-d H:i:s'); // "2010-06-15 09:56:36"
If you are using or have access to Carbon you could do this:
$timezone = 'America/Los_Angeles';
$now = Carbon::now()->tz($timezone)->toDateTimeString();
echo $now;
.
echo date('r');
putenv('TZ=PST');
echo date('r');
To convert a date/time between timezones:
include ("Date.php");
$d = new Date("2010-06-21 10:59:27"); // initialize object
$d->setTZByID("GMT"); // set local time zone
$d->convertTZByID("PST"); // convert to foreign time zone
echo $d->format("%A, %d %B %Y %T"); // retrieve converted date/time
I'm currently having an issue wrapping my brain around the notion of converting my MySQL time to a specific timezone, depending on the user's settings.
All of my MySQL times are stored in UTC time, in the following format:
2009-11-08 17:06:40
Once I query the time, I'm not quite sure how to convert it to the appropriate timezone using PHP.
Thus, in the example above, I'd like to display:
2009-11-08 09:06:40
Here's what I currently have (which probably needs to be fixed):
$sql = 'SELECT date FROM mytable';
require("connection.php");
$result = mysql_db_query($DBname,$sql,$link);
while($row = mysql_fetch_assoc($result)) {
$dt_obj = new DateTime($row['date']);
$dt_obj->setTimezone(new DateTimeZone('PST'));
echo $dt_obj;
echo "<br>";
}
First off, I get the following error:
Catchable fatal error: Object of class DateTime could not be converted to string
Secondly, I am confused as to whethere I'm setting it up properly to display the time in the correct timezone anyway (in this case, PST).
Any suggestions on how to do this would be greatly appreciated. Thanks!
UPDATE:
I took GZipp's advice, and modified the code to look like:
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $dt_obj->format('Y-m-d H:i:s');
However, it's displaying my time (using the example from above) as:
2009-11-08 15:06:40
Any ideas on what would be causing this?
I think this is what you're after;
$dt_obj = new DateTime($row['date']);
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $dt_obj->format('Y-m-d H:i:s');
Use the List of Supported Timezones.
Update to edited question: To ensure that PHP sees the time from the database as UTC time, do something like this:
$row['time'] = '2009-11-08 09:06:40';
$dt_obj = new DateTime($row['time'], new DateTimeZone('UTC'));
echo 'UTC: ' . $dt_obj->format('Y-m-d H:i:s') . '<br />'; // UTC: 2009-11-08 09:06:40
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo 'Los Angeles: ' . $dt_obj->format('Y-m-d H:i:s'); // Los Angeles: 2009-11-08 01:06:40
I may be wrong, but it looks like it's complaining about your echo $dt_obj; statement. You might try echoing the result of DateTime::format();
EDIT: For your new question I would guess that your default format is set to PST already, so when the new date is created it's create with that timezone, thereby setting the timezone changes nothing. You might check and see if that's the case. You also might look at date_default_timezone_set('<tz>');