PHP Change 24 hour datetime using strtotime - php

I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600

You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);

You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));

Related

PHP - Convert string to date in

I am getting a string like Wed Sep 28 2016 01:00:00 GMT+0500 (PKT) and I need to convert it to 2016-09-28 01:00:00
I have tried this
$startTime = strtotime($updatedData['start']);
echo $time = date("Y-m-d H:i:s",$startTime);
but it returns me 2016-09-27 20:00:00
You could change it to use DateTime:
$startTime = new DateTime('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
echo $startTime->format('Y-m-d H:i:s');
DateTime keeps the timezone you give him.
Live Example: https://3v4l.org/UTltO
#copynpaste solution is nice and straight forward but I will still share my solution by using Carbon.
Carbon is a library included together with laravel and here is the
documentation.
$carbon = new Carbon('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
$carbon->format('Y-m-d H:i:s');
echo $carbon;
it will come out the result same as DateTime
2016-09-28 01:00:00
So what carbon nice is you can just add day, minute, second and etc by just a very minimal code, here is an example:
$carbon->addDays(1);
echo $carbon;
//result
2016-09-29 01:00:00
Try this:
$startTime = strtotime($updatedData['start']);
$time = date("Y-m-d H:i:s",$startTime);
echo date( "Y-M-d H:i:s", strtotime( $time) + 5 * 3600 );
It returns UTC time and you do need to add 5 hours to it. Also a quick suggestion. You can use Carbon for handling the date time.
You can set the desired time zone before converting. Please see the below code as a reference.
date_default_timezone_set("Asia/Bangkok");
$str = 'Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)';
$startTime = strtotime($str);
echo $time = date("Y-m-d H:i:s",$startTime);

What create_from_format to use for timestamps in php

I am working on a PHP file were I am pulling in timestamps from a database that come out as
Tue Sep 22 16:11:12 EDT 2015
Mon Nov 16 07:42:31 EST 2015
I was wondering how to convert dates of this format into unix time format.
You can use the DateTime object
$dateTime = new DateTime('Tue Sep 22 16:11:12 EDT 2015');
echo $dateTime->getTimestamp();
The result would be 1442952672
If you look at the PHP manual for \DateTime::createFromFormat() or date(), you will find tables of codes accepted by \DateTime::createFromFormat(). Looking at them you will see that you need the following:-
$date = \DateTime::createFromFormat('D M d H:i:s T Y', 'Tue Sep 22 16:11:12 EDT 2015');
echo $date->getTimestamp();
Demo.
The 'd' in the format string may need to be a 'j' if day numbers < 10 are single digit.

how to convert human date to unix by strotime

Can anyone help , i want to to make simple script as anyone insert human date like 2013 - 9 - 25 19:10:00 P.M to milliseconds Unix .
is it possible using strtotime.
I want anyone to show me how to make the whole human date (2013 - 9 - 25 19:10:00 P.M ) be in one variable so i can convert it then echo it
You can use strtotime or timestamp
timestamp example
$dt = new DateTime("Fri Dec 18 2013 14:25:00 GMT-0600 (MST)");
$ts = $dt->getTimestamp();
Strtotime example
$ts = strtotime("Fri Dec 09 2013 14:25:00 GMT-0600 (MST)");
echo date("r", $ts);
In your case:
$timestamp = strtotime('2013-9-25 19:10:00 P.M');

Convert string of UTC time to more readable format

I'm receiving a string containing date and time (in UTC format) for when something was created. The string looks like this: "Wed Mar 13 14:10:20 +0000 2013". Now, I need to convert that to a more readable format. Something like this "14:10, 13 Mar" or preferably "1 hour ago", "1 week ago" etc.
How do I do this?
Thanks.
Use the DateTime object. The constructor should be able to parse that timestamp, then you can mainpulate the date and output in any format you wish:
$date = new dateTime('Wed Mar 13 14:10:20 +0000 2013');
echo $date->format('h:i, d-M');
For relative times, see: Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon"
Try this:
$dt = 'Wed Mar 13 14:10:20 +0000 2013';
echo date( 'h:i, d-M', strtotime( $dt ) );
The time difference will require more code.

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Categories