I've a date in the format dd/mm/YYYY. Eg: 25/06/2015.
I want convert it to timestamp. I've added the following code to implement this;
$timestamp = strtotime( str_replace( '/', '-', '25/06/2015' ) );
It creates timestamp, but when I convert that timestamp I can see that it is one day before. When I execute the above code, I got the timestamp value "1435183200". When I convert this I got the previous date "24/06/2015".
If anybody knows the solution to fix this, please help.
I think you have the default timezone configured in PHP. Try switching your timezone. Use:
date_default_timezone_set('your_timezone_here');
For a list of supported timezones, go to http://php.net/manual/en/timezones.php
Related
I am not sure how to convert timestamp into ISO-8601 and also apply UTC.
However, I am looking for following output:
2017-09-23T01:08:36.6437128Z ( Format = ISO-8601. Timezone = UTC)
I have tried couple of ways to get this but I am not getting it so anyone know how to get this. As this must be very quick and easy fix but I am missing something and that's the reason I am not getting this output.
Any help would be appreciated:)
Thanks
First of all, your desired format is not ISO 8601 compliant because you're including microseconds.
Use date_default_timezone_set to get UTC and date to easily format a timestamp.
Demo: https://3v4l.org/aRKqi
$timestamp = 1506086214;
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// ISO 8601 (does NOT include microseconds)
echo date("c", $timestamp) . PHP_EOL;
// custom format e.g. 2017-09-23T01:08:36.6437128Z
echo date("Y-m-d\TH:i:s.u\Z", $timestamp) . PHP_EOL;
2017-09-22T13:16:54+00:00
2017-09-22T13:16:54.000000Z
In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.
I am trying to get the current date/time using Data Type: ISODateTime and Format: YYYY-MM-DDTHH:MM:SS e.g. 2012-02-06T08:35:30. I searched how I would do this in PHP and found that I can use;
$formatedDate = date("c");
Although the output of this is almost correct is not quite what I need and I can't figure out how to alter it, the current output of this is;
2014-07-01T10:53:10+02:00
My problem is I need to remove the "+02:00" and also this time is an hour ahead of my local time, which is what I need. Therefore, in this example, I would require;
2014-07-01T09:53:10
Any help would be really appreciated. Thanks.
You could just format the date manually.
$formattedDate = date('Y-m-d\TH:i:s');
To get your local time, use date_default_timezone_set() to set the appropriate timezone (before declaring $formattedDate).
// Change 'America/New_York' to your timezone
date_default_timezone_set('America/New_York');
See demo
To find your timezone, see the List of Supported Timezones.
I've been struggling to get an exact answer for this question. There are many that are close to what I'm wanting but seem to still be just off. The application of this is to ensure that a booking can't be made for a past date.
I have a form which has an input for time & another for date. Firstly, I wan't to take both of these inputs & convert them to a timestamp.
This code returns nothing
$time_date = sprintf("%s %s", $pDate, $pTime);
$objDate = DateTime::createFromFormat('H:ia d/m/Y', $time_date);
$stamp = $objDate->getTimestamp();
echo $stamp;
So I've have tried using something like this
$pDate = $_POST['pDate'];
$pTime = $_POST['pTime'];
$full_date = $pDate . ' ' . $pTime;
$timestamp = strtotime($full_date);
echo $timestamp;
But for some reason it is returning an incorrect timestamp. (i've been using an online converter) 02/06/2014 as date & 12:23am as time, is not 1401625380. This according to the converter is Sun, 01 Jun 2014 12:23:00 GMT.
Does someone have working code for returning a timestamp of both time & date inputs?
Secondly I want to compare this timestamp with a specified one & check to see if it is greater than. I've created a timestamp for my timezone with this
$date = new DateTime(null, new DateTimeZone('Pacific/Auckland'));
$cDate = $date->getTimestamp();
echo $cDate;
and will simply have an if statement which compares the two and echos the appropriate message.
I feel as though there are multiple question on here that are ALMOST what I'm wanting to achieve but I can't manage to get them working. Apologies for the near duplicate.
Note: I'm using ajax to post form data (if this could possibly interfere).
Your second code snipped is correct. Assuming it's in datetime format (Y-m-d H:i:s).
From php manual about strtotime():
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
Check your PHP default time zone with date_default_timezone_get() function.
To compare two dates, be sure they both are in same time zones.
For datetime inputs I personally use jQuery UI timepicker addon.
you receiving the time and date in string format - so i don't believe the ajax can interfere.
as for your question:
first of all - find out what is the locale timezone of your server. you can do it by this function: date_default_timezone_get.
if the answer doesn't suit you - you can use its "sister": date_default_timezone_set, and change it to whatever value you need (like 'Pacific/Auckland' - see the documentation there). it is also recommended to return it to the original value after you finish your stuff.
i believe fixing your locale timezone will solve your issue.
I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with timeĀ“() function to ensure that it's OK.
Good luck