I have a problem in extracting time from $timestamp which something looks like
this $timestamp = "2017-05-03 11:47:48";
my question is: how to extract time from above $timestamp like this 11:47 AM
my current time zone is Asia/Kolkata
First make sure you installed carbon, refer to https://github.com/briannesbitt/Carbon.
$timestamp = "2017-05-03 11:47:48";
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2017-05-03 11:47:48', 'Asia/Kolkata');
echo $date->format("H:i A");
First of all this
$timestamp = "2017-05-03 11:47:48";
is not timestamp, Its a date time value in some specific format. To change it to your required format you can try following code in Php:
echo date('H:i A', strtotime($timestamp));
here H is used for 24 Hour format, use h for 12 hours format.
Hope this will help you out.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$timestamp = "2017-05-03 11:47:48";
$dateObj=date_create_from_format("Y-m-d H:i:s", $timestamp);
echo $dateObj->format("H:i A");
Output:
11:47 AM
Related
I want to get the timestamp of a day/time for eg
17/12/2014 8pm
Currently I am doing
$curtime = strtotime(date("Y-m-d H:i:s"));
which is giving me the current timestamp but I need to be of 8pm.
Any help is highly appreciated. Thanks in advance.
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:
$curtime = strtotime('today 8pm');
If you test this with date:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:
$date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
if (!empty($date)) {//returns false if can't create date
$timestamp = $date->getTimestamp();
//echo date('d/m/Y H:i:s', $timestamp);
}
I have this line of code I wrote to create a formatted timestamp on emails from my contact us page.
It is working fine, but I'm wondering if it is written poorly and could be reduced into more efficient code? It feels wrong calling date() three times in one line. I'm not a developer by trade.
$timestamp = date('m-d-Y')." ".date('h:i A', strtotime(date('H:i:s')));
which results in: 05-28-2014 03:49 PM
Thoughts?
When you need the current timestamp, you can use,
$timestamp=date("m-d-Y h:i A");
When you need to format the timestamp you fetched from database or other means, you have to use strtotime.
$format_timestamp=date("Y-m-d H:i:s", strtotime($timestamp)); // I just convert your format to YYYY-MM-DD HH:MM:SS format.
Edit:
When you need to subtract x hours from the current time, use
$timestamp=date("m-d-Y h:i A", strtotime("-4 hour"));
Some more examples,
$timestamp=date("m-d-Y h:i A", strtotime("+2 hour")); // Adds 2 hours
$timestamp=date("m-d-Y h:i A", strtotime("+1 day")); // Adds 1 Day
You can simplify making:
$timestamp = date('m-d-Y h:i A');
or
$timestamp = gmdate("M d Y H:i:s");
For more information, see:
PHP Manual
date http://www.php.net/manual/en/function.date.php
gmdate http://www.php.net/manual/en/function.gmdate.php
I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.
I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];
You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date
http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm
You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));
go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php
You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM