I have a date time like 30-10-2018 06:00:00 pm, Now I want to convert it into UTC timestamp, so I am trying to convert this to strtotime and multiplying it by 1000. But it leading to 6hrs difference in the resulted date
$min_date = "30-10-2018 06:00:00 pm";
$max_date= "30-10-2018 07:00:00 pm";
echo $minDate= strtotime($min_date) * 1000;
echo "<br>";
echo $maxDate= strtotime($max_date) * 1000;
What you want to use is a built-in PHP function known as "gmdate."
Let's use your first timestamp, $min_date, in our example of using gmdate to convert the datetime string to a UTC timestamp:
$min_date = "30-10-2018 06:00:00 pm";
//Use 'gmdate', which accepts a formatting string and time() values as arguments.
$utc_min_date = gmdate("d-m-Y H:i:s", strtotime($min_date));
echo "<p>Before: ".$min_date."</p>";
//Produces: "Before: 30-10-2018 06:00:00 pm"
echo "<p>UTC: ".$utc_min_date."</p>";
//Produces: "UTC: 30-10-2018 23:00:00"
If you're wanting a purely numerical representation, akin to a time() stamp, you can simply convert the resulting UTC timestamp.
$numerical_utc_min_date = strtotime($utc_min_date);
echo "<p>Numerical UTC timestamp: " . $numerical_utc_min_date . "</p>";
//Produces: "Numerical UTC timestamp: 1540958400"
Related
I want to check the date input and convert it to 24h format. For example the datetime is:
$date="2021-02-5 11:45:00 AM"
and after i check it with this condition:
if (preg_match("/am|pm|AM|PM/", $date))
{
$date=date('Y-m-d H:i:s',strtotime("$date"));
}
and my output should be like this however it does not return a date format like that:
$date="2021-02-5 11:45:00";
How can i fix this problem?
Converting time format
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));
You don't need regex to check if it is 24 hours or 12 hours. you can simply pass the date to strtotime function to get a timestamp of the date and then format the timestamp with date function.
$date="2021-02-5 11:45:00 AM";
$date=date('Y-m-j H:i:s',strtotime($date));
echo $date; // 2021-02-5 11:45:00
date_create_from_format('Y-m-j H:i:s A', $date);
It will return a DateTime object, and you can get right date you want, and it will return false if date string don't match the pattern.
DateTime Object
(
[date] => 2021-02-05 11:45:00.000000
[timezone_type] => 3
[timezone] => UTC
)
date_create_from_format ( string $format , string $time , DateTimeZone $timezone = ? ) : DateTime
With strtotime you have no control over the formats. To be on top of what is happening better use the feature-rich DateTime class. First convert the string to a DateTime object and then format it to whatever you need.
<?php
$dt = "2021-02-5 11:45:00 pm";
if (preg_match('/am$|pm$/i', $dt)) // is there an Ante meridiem/Post meridiem suffix?
{
$dtFormat = 'Y-n-j H:i:s a';
}
else
{
$dtFormat = 'Y-n-j H:i:s';
}
$ts = DateTime::createFromFormat($dtFormat, $dt);
$date = $ts -> format('Y-m-d H:i:s');
// echo 'Result: '. $date;
I have a variable called $end_date which gives me the date in epoch format like this: 1539129600. This translates to Wednesday 10 October 2018 00:00:00.
The variable returns different dates, but the time is always the same 00:00:00. Is it possible to take this variable, set the time to 23:59:59 and save it back to a new variable called $end_date_time containing the same date and the new time 23:59:59 in epoch format.
You can do this with the DateTime class, using setTimestamp to convert the timestamp and then setTime to set the time of day, finally getting the new timestamp with format:
$end_date = 1539129600;
$end_of_day = new DateTime(null, new DateTimeZone('UTC'));
$end_of_day->setTimestamp($end_date);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
$end_of_day->setTime(23,59,59);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
echo (int)$end_of_day->format('U');
Output:
2018-10-10 00:00:00
2018-10-10 23:59:59
1539215999
Demo on 3v4l.org
I have date format like '25 May 2016 10:45:53:567'.
I want to convert into the time stamp.
strtotime function returns empty.
$date = '25 May 2016 10:45:53:567';
echo strtotime($date);
// returns empty
When I removed the milliseconds, it's working.
$date = '25 May 2016 10:45:53';
echo strtotime($date);
// returns 1464153353
Please sort out my issue. Thanks in advance.
Use DateTime:
$date = DateTime::createFromFormat('d M Y H:i:s:u', '25 May 2016 10:45:53:000');
echo $date->getTimestamp();
// 1464165953
// With microseconds
echo $date->getTimestamp().'.'.$date->format('u');
// 1464165953.000000
Split string:
$date = '25 May 2016 10:45:53:001';
preg_match('/^(.+):(\d+)$/i', $date, $matches);
echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
echo 'milliseconds: ' . $matches[2] . PHP_EOL;
// timestamp: 1464162353
// milliseconds: 001
Use Datetime instead of date and strtotime.
//using date and strtotime
$date = '25 May 2016 10:45:53:000';
echo "Using date and strtotime: ".date("Y-m-d H:i:s.u", strtotime($date));
echo "\n";\
//using DateTime
$date = new DateTime();
$date->createFromFormat('d M Y H:i:s.u', '25 May 2016 10:45:53:000');
echo "Using DateTime: ".$date->format("Y-m-d H:i:s.u");
// since you want timestamp
echo $date->getTimestamp();
// Output
// Using date and strtotime: 1969-12-31 16:00:00.000000
// Using DateTime: 2016-05-28 03:25:22.000000
Example
I'm having a hard time getting dates to stick in variables. When I when I'm tyring to subtract months from a current date it is giving me 0 back (defaulting back to 1-1-1970).
Any thoughts on what I could be doing wrong?
PHP:
$progress = 5;
$initialDate = date('m-d-Y');
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-01-Y', strtotime("-$progress months", strtotime(date('m-d-Y',$initialDate))));
echo "date: " . $date . "<br>";
output:
ini date: 12-01-2014
date: 08-01-1969
The 2nd argument to date() must be in internal Timestamp format, i.e. "number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)".
strtotime() is really intended to convert a user input into internal date format. If you want to do math with dates, it is better to use a more precise method. I would suggest reading up on date_add() here: http://php.net/manual/en/datetime.add.php
However, the simplest fix to your code is this:
$progress = 5;
$initialDate = time(); // current timestamp, including seconds
echo "initialDate: $initialDate<br>";
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-d-Y', strtotime("-$progress months", strtotime(date('Y-01-d',$initialDate))));
echo "date: " . $date . "<br>";
Note that I used Y-01-d format for the date fed to strtotime, to go back to the 1st of the month before doing the date math, for two reasons:
Y-m-d format has no danger of being interpreted incorrectly by strtotime, unlike m/d/y and d/m/y.
If you are going back 5 months from Jul 30 to Feb 1, it is safer to have Jul 1 as the intermediate step, not Feb 28. "Safer" meaning you don't even need to test if strtotime handles Feb 28 properly.
You can do this using the DateTime object as well. I find it a little more reliable myself. You basically just initiate a DateTime object and then use its ::sub function to subtract a DateInterval in the amount of $progress months.
$progress = 5;
$initialDate = new DateTime(date("Y-m-01"));
echo "ini date: ".$initialDate->format("m-01-Y")."<br />";
$date = $initialDate->sub(new DateInterval("P".$progress."M"));
echo "date: ".$date->format("m-01-Y")."<br />";
Output:
ini date: 12-01-2014
date: 07-01-2014
I have a timestamp stored in a session (1299446702).
How can I convert that to a readable date/time in PHP? I have tried srttotime, etc. to no avail.
Use PHP's date() function.
Example:
echo date('m/d/Y', 1299446702);
strtotime makes a date string into a timestamp. You want to do the opposite, which is date. The typical mysql date format is date('Y-m-d H:i:s'); Check the manual page for what other letters represent.
If you have a timestamp that you want to use (apparently you do), it is the second argument of date().
I just added H:i:s to Rocket's answer to get the time along with the date.
echo date('m/d/Y H:i:s', 1299446702);
Output: 03/06/2011 16:25:02
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new \DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
result: 2016-06-07 14:29:00
Other time zones:
Africa
America
Antarctica
Arctic
Asia
Atlantic
Australia
Europe
Indian
Pacific
Others
If you are using PHP date(), you can use this code to get the date, time, second, etc.
$time = time(); // you have 1299446702 in time
$year = $time/31556926 % 12; // to get year
$week = $time / 604800 % 52; // to get weeks
$hour = $time / 3600 % 24; // to get hours
$minute = $time / 60 % 60; // to get minutes
$second = $time % 60; // to get seconds
If anyone wants timestamp conversion directly to a DateTime object, there's a simple one-liner:
$timestamp = 1299446702;
$date = DateTime::createFromFormat('U', $timestamp);
Following #sromero comment, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.
$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone('UTC'); // not needed, 3rd parameter is ignored
You may check PHP's manual for DateTime::createFromFormat for more info and options.
Try this one:
echo date('m/d/Y H:i:s', 1541843467);
$epoch = 1483228800;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:
Format Output
r ----- Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date)
c ----- 2017-03-15T12:00:00+01:00 (ISO 8601 date)
M/d/Y ----- Mar/15/2017
d-m-Y ----- 15-03-2017
Y-m-d H:i:s ----- 2017-03-15 12:00:00
Try it.
<?php
$timestamp=1333342365;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
You can try this:
$mytimestamp = 1465298940;
echo gmdate("m-d-Y", $mytimestamp);
Output :
06-07-2016
Unless you need a custom date and time format, it's easier, less error-prone, and more readable to use one of the built-in date time format constants:
echo date(DATE_RFC822, 1368496604);
echo date("l M j, Y",$res1['timep']);
This is really good for converting a unix timestamp to a readable date along with day. Example:
Thursday Jul 7, 2016
echo 'Le '.date('d/m/Y', 1234567890).' à '.date('H:i:s', 1234567890);
I have used this:
<?php echo date('d/m/Y H:i a', $row['start_time']); ?>