How to convert timestamp format while using codeigniter - codeigniter-3

I am using-
echo $post->entry_time;
this will echo a proper timestamp like - 2015-12-09 13:47:46.
i want to format it with 16 December 2015 with time.

In below i have created function for get UTCmills to Date , just add function in helper.php and you can use in view, controller and model,
/*
#return convert UTC milisec to date format
#param $milisec , $formate
#explain 1 $milisec convert to date
#explain 2 $formate convert as per date format
*/
function UTCmillsToDate($milisec, $formate = "Y-M-D") {
$mil = $milisec;
$seconds = $mil / 1000;
if ($formate == "YMD") {
return date("Ymd", $seconds);
} else if ($formate == "Y-M-D") {
return date("Y-m-d", $seconds);
} else if ($formate == "d/m/Y") {
return date("d/m/Y", $seconds);
} else if ($formate == "Y-m-d H:i:s") {
return date("Y-m-d H:i:s", $seconds);
}else if ($formate == "d-m-Y H:i:s") {
return date("d-m-Y H:i:s", $seconds);
}
else if ($formate == "s") {
return date("r", $seconds);
} else {
return date("d-m-Y", $seconds);
}
}
Example :
1 ==> UTCmillsToDate('1449750420')
Output: 2015-10-12
2 => UTCmillsToDate('1449750420','Y-m-d H:i:s')
Output: 2015-10-12 12:27:00

Related

Flutter how to create a loop in else if

i got a chart that display months (in french) when i go below the month == 1, i would like to go back to my current month and not see my 'error'.
Is there a method to make it ?
String numericToStringMonth(int month) {
if (month < 1 || month > 12) {
return 'error';
} else if (month == 1) {
return 'Janvier';
} else if (month == 2) {
return 'Février';
} else if (month == 3) {
return 'Mars';
} else if (month == 4) {
return 'Avril';
} else if (month == 5) {
return 'Mai';
} else if (month == 6) {
return 'Juin';
} else if (month == 7) {
return 'Juillet';
} else if (month == 8) {
return 'Août';
} else if (month == 9) {
return 'Septembre';
} else if (month == 10) {
return 'Octobre';
} else if (month == 11) {
return 'Novembre';
} else if (month == 12) {
return 'Décembre';
} else {
return 'error';
}
}
String numericToStringMonth(int month) {
// make list of months for easier access
List<String> months = [
'Janvier',
'Février',
'Mars',
'Avril',
'Mai',
'Juin',
'Juillet',
'Août',
'Septembre',
'Octobre',
'Novembre',
'Décembre'
];
int currentMonth = month - 1;
try {
if (currentMonth > 11) {
throw Exception('Month must be between 1 and 12');
} else if (currentMonth < 0) {
// setting month to the first one when value goes below 0, add your logic here
currentMonth = 1;
}
return months[currentMonth];
} catch (e) {
print(e);
}
return '';
}

Compare two time in flutter

I would like to compare last_uploaded_time with current time.
How to check whether the two time is more or less than one minute?
bool compareTime(String starts) {
print(starts);
var start = starts.split(":");
DateTime currentDateTime = DateTime.now();
String currentTime =
DateFormat(DateUtil.TIME_FORMAT).format(currentDateTime);
print(currentTime);
var end = currentTime.split(":");
DateTime initDateTime = DateTime(
currentDateTime.year, currentDateTime.month, currentDateTime.day);
var startDate = (initDateTime.add(Duration(hours: int.parse(start[0]))))
.add(Duration(minutes: int.parse(start[1])));
var endDate = (initDateTime.add(Duration(hours: int.parse(end[0]))))
.add(Duration(minutes: int.parse(end[1])));
if (currentDateTime.isBefore(endDate) &&
currentDateTime.isAfter(startDate)) {
print("CURRENT datetime is between START and END datetime");
return true;
} else {
print("NOT BETWEEN");
return false;
}
}
Output
I/flutter (12908): 01:16
I/flutter (12908): 01:40
I/flutter (12908): NOT BETWEEN
difference
not exactly working with minutes and seconds so you can use some custom algorithm like
import 'package:intl/intl.dart';
class IntelModel { static String timeSinceDate(DateTime date) {
final now = DateTime.now();
final difference = now.toLocal().difference(date.toLocal());
if ((now.day - date.day) >= 8) {
return 'A few weeks ago';
} else if ((now.day - date.day) >= 1) {
return '${(now.day - date.day)} days ago';
} else if (now.day == date.day) {
if (now.hour > date.hour) {
if ((now.hour - date.hour) >= 2) {
if (now.minute == date.minute) {
return '${(now.hour - date.hour)} hours ago';
} else {
var mins = now.minute - date.minute;
if (mins > 1) {
return '${(now.hour - date.hour)} h ${(now.minute - date.minute)} minutes ago';
} else {
return '${(now.hour - date.hour) - 1} h ${60 + mins} minutes ago';
}
}
} else if ((now.hour - date.hour) == 1) {
int timeMin = now.minute + (60 - date.minute);
if (timeMin == 60) {
return '1 hours ago';
} else if (timeMin >= 60) {
return '1 h ${timeMin - 60} mins ago';
} else {
return '$timeMin minutes ago';
}
}
} else if (now.hour == date.hour) {
if (now.minute > date.minute) {
return '${(now.minute - date.minute)} minutes ago';
} else if (date.minute == now.minute) {
return '${(now.second - date.second)} seconds ago';
}
}
} else {
return 'Error in time';
} } }
void main() {
String getDifference(DateTime date){
Duration duration = DateTime.now().difference(date);
String differenceInMinutes = (duration.inMinutes).toString();
return differenceInMinutes;
}
String str = getDifference(DateTime.parse('2021-09-24'));
print (str);
}
i tried above code on dartpad, you can use this to compare tow dateTime variables. As per above example, you can get more options to compare for eg duration.inDays,duration.inHours,duration.inSeconds etc.

How to compare timestamp to current time in flutter

This is my timestamp = "2020-05-29T17:43:39.622832+05:30". How can I pass it to a function readTimeStamp (it will give me error of not type of int)?
date = DateTime.parse(bookDetails.timestamp);
print(readTimestamp(date));
String readTimestamp(int timestamp) {
var now = DateTime.now();
var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var diff = now.difference(date);
String time = '';
if (diff.inSeconds <= 0 ||
diff.inSeconds > 0 && diff.inMinutes == 0 ||
diff.inMinutes > 0 && diff.inHours == 0 ||
diff.inHours > 0 && diff.inDays == 0) {
} else if (diff.inDays > 0 && diff.inDays < 7) {
if (diff.inDays == 1) {
time = diff.inDays.toString() + ' DAY AGO';
} else {
time = diff.inDays.toString() + ' DAYS AGO';
}
} else {
if (diff.inDays == 7) {
time = (diff.inDays / 7).floor().toString() + ' WEEK AGO';
} else {
time = (diff.inDays / 7).floor().toString() + ' WEEKS AGO';
}
}
return time;
}
This is my function to return value like 3 day ago and all.
DateTime.parse returns a DateTime. readTimestamp appears to expect the number of seconds since the epoch, so you just need to use DateTime.millisecondsSinceEpoch and convert milliseconds to seconds:
print(readTimestamp(date.millisecondsSinceEpoch ~/ 1000));
Personally, if you control the readTimestamp function, I would rename its ambiguous timestamp argument to secondsSinceEpoch to make it clear what it expects. Even better would be to change its argument to take a DateTime directly instead of doing unnecessary DateTime <=> milliseconds <=> seconds conversions.
bool isAfterToday(Timestamp timestamp) {
return DateTime.now().toUtc().isAfter(
DateTime.fromMillisecondsSinceEpoch(
timestamp.millisecondsSinceEpoch,
isUtc: false,
).toUtc(),
);
}

How to add range validation for year in flutter

I've tried to add year range validation it must be between 1850 to the current year. i've tried but it's not working.
Here is the code I've tried
String validateestablishedyear(String value) {
var date = new DateTime.now();
int currentYear = date.year;
int userinputValue = 0;
print('currentYear = $currentYear');
print('input value = $value');
print('value = ${value is int}');
print('value = ${value is String}');
if (value is String && value.length == 0) {
return "Established Year is Required";
} else {
userinputValue = int.parse(value);
}
if (userinputValue < 1850 && userinputValue >= currentYear) {
return "Year must be between 1850 and $currentYear";
}
return null;
}
Just change your code as below,
if (userinputValue < 1850 || userinputValue >= currentYear) {
return "Year must be between 1850 and $currentYear";
}
You're logic is wrong. you should have use || instead of &&

Time format in Quartz.net tables

The Start_time field in Database table(dbo.QRTZ_TRIGGERS) shows this value 635371706123133677
What format is this and how can it be converted to human readable format
This is the from the StdAdoDelegate.cs class:
AddCommandParameter(cmd, "triggerStartTime", GetDbDateTimeValue(trigger.StartTimeUtc));
And GetDbTimeValue is
public virtual object GetDbDateTimeValue(DateTimeOffset? dateTimeValue)
{
if (dateTimeValue != null)
{
return dateTimeValue.Value.UtcTicks;
}
return null;
}
So basically DateTimeOffset.UtcTicks
And this is the code used to convert that value back:
public virtual DateTimeOffset? GetDateTimeFromDbValue(object columnValue)
{
if (columnValue != null && columnValue != DBNull.Value)
{
var ticks = Convert.ToInt64(columnValue, CultureInfo.CurrentCulture);
if (ticks > 0)
{
return new DateTimeOffset(ticks, TimeSpan.Zero);
}
}
return null;
}